## Python: Any optimized way to check that 2 lists increase together?

Let's assume we have 2 lists a = [1,2,4,3,5] and b = [103,122,800,500,1000]

is there an optimized way that I can check that they are "increasing together"?

My current solution, employs a loop:

for i in range(1,len(a)):
if (a[i-1] < a[i] and b[i-1] > b[i]) or (a[i-1] > a[i] and b[i-1] < b[i]):
print('wrong')

Is there a better way?

Notes:

• The solution does not need to be list specific (really any data structure would work)
• The two iterables do not need to increase by the same number of units, just have to increase together.

You can't really get any faster than O(n), but you could make your code a bit shorter and maybe more readable by using numpy.diff and comparing the sign of the diffs of a and b:

>>> from numpy import diff, sign
>>> a, b = [1,2,4,3,5], [103,122,800,500,1000]
>>> sign(diff(a))
array([ 1,  1, -1,  1])
>>> all(sign(diff(a)) == sign(diff(b)))
True
>>> a, b = [1,2,4,3,5], [103,122,800,500,100]
>>> all(sign(diff(a)) == sign(diff(b)))
False

The downside of this solution is that it does not use lazy-evaluation, i.e. it calculates and compares the entire sign(diff(...)) array even if the "increasingness" of a and b differs in the very first position. If the list is very long, you should consider using another approach.

Python Compare Two Lists, and return a value based on the arguments passed. The cmp () function is a built-in method in Python used to compare the elements of two lists. The function is also used to compare two elements and return a value based on the arguments passed. This value can be 1, 0 or -1. Note: cmp () build to function for python version 2, In python version 3 it is not available.

In terms of O(order notation), you can't get better than linear, assuming lists don't have some order. But, you can use some python compiler like cython, numba to speed up your code. Your code using numba:

import numpy as np
import numba as nb

@nb.njit()
def vary_together(a, b):
for i in range(1,len(a)):
if (a[i-1] < a[i] and b[i-1] > b[i]) or (a[i-1] > a[i] and b[i-1] < b[i]):
return False
return True

You have to use large lists to see the performance benefit. For example, if:

a = np.array([randint(0,100) for i in range(10000000)])

Then,

vary_together(a, a)  # a as both arguments so as to make it complete the loop

Has has the performance comparison to your solution as :

Your solution: 8.09s vary_together: 0.2 (on second run to discount for compile time).

If you need to run the code again and again in the script, do cache=True in the nb.njit decorator.

How can I compare two ordered lists in python?, How do you check if two lists are identical in Python? Python | Intersection of two lists Intersection of two list means we need to take all those elements which are common to both of the initial lists and store them into another list. Now there are various ways in Python, through which we can perform the Intersection of the lists.

We can use the lazy evaluation provided by python iterators, meaning we don't need to continue traversing both lists ( structures ) once they don't have the same variation sign

def compare_variation( a, b ):
a_variations = ( a[ i - 1 ] < a[ i ] for i in range( 1, len( a ) ) )
b_variations = ( b[ i - 1 ] < b[ i ] for i in range( 1, len( b ) ) )
return all( x == y for x, y in zip( a_variations, b_variations  ) )

Difference Between Two Lists using Python Set & Without Set, or not, a == b should give you true / false with ordering taken into account. Here, c will contain an array with 3 elements all of which are true (for your example). The built-in Python sorting is stable, so the two 1 s don't cause a problem. >>> l1 = [3, 2, 4, 1, 1] >>> l2 = ['three', 'two', 'four', 'one', 'second one'] >>> zip(*sorted(zip(l1, l2))) [ (1, 1, 2, 3, 4), ('one', 'second one', 'two', 'three', 'four')] share. Share a link to this answer. Copy link. |.

Here is a way with list comprehension:

c = [(a[x + 1] - a[x]) * (b[x + 1] - b[x]) for x in range(len(a) - 1)]
if any([x < 0 for x in c]):
print('Wrong')

Comparing all the previous approaches (except for the numba one), tobias_k's answer looks like most efficient when dealing with large enough lists (at list 40 elements roughly).

Python, How do you compare two lists in python and return Non matches? Python extend () Vs append () If you need to add an element to the end of a list, you can use the append () method. a1 = [1, 2] a2 = [1, 2] b = (3, 4) # a1 = [1, 2, 3, 4] a1.extend (b) # a2 = [1, 2, (3, 4)] a2.append (b) To learn more, visit list append () method.

Python, This article deals with the task of ways to check if two unordered list contains exact similar elements in exact similar position, i.e to check if two lists are exactly equal. The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical Python | Check if two lists have any element in common · Python | Check  Python Functions: List comprehension, Map and Filter. To make a more broad comparison we will also benchmark against three built-in methods in Python: List comprehensions, Map and Filter. List comprehension: List comprehensions are known to perform, in general, better than for loops as they do not need to call the append function at each iteration.

Optimization tricks in Python: lists and tuples, Input : a = [1, 2, 3, 4, 5] b = [5, 6, 7, 8, 9] Output : True Input : a=[1, 2, 3, 4, Using traversal in two lists, we can check if there exists one common element at least in them. a positive integer, if it does not contains any positive integer, then it returns 0. Please Improve this article if you find anything incorrect by clicking on the  When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security. List. A list is a collection which is ordered and changeable. In Python lists are written with square brackets.

11. Lists, An overview of optimization tricks for lists and tuples. a = (1,2,3) >>> a[0] = 10 Traceback (most recent call last): File When creating an empty tuple Python points to already preallocated one, in such way that any empty This over-​allocates proportional to the list size, making room for additional growth. xrange is a generator object, basically equivalent to the following Python 2.3 code: def xrange (start, stop=None, step=1): if stop is None: stop = start start = 0 else: stop = int (stop) start = int (start) step = int (step) while start < stop: yield start start += step. Except that it is implemented in pure C.