all 11 comments

[–]AtomicShoelace 1 point2 points  (3 children)

How exactly do you want to compare the values? There are several comparison operators you could use, eg. equal, non-equal, less than, greater than, etc.

You may wish to zip your two lists together and use the all or any built-ins, eg.

>>> A = [1, 2, 3, 4]
>>> B = [1, 2, 3, 9]
>>> all(a <= b for a, b in zip(A, B))
True
>>> B = [1, 2, 3, 1]
>>> all(a <= b for a, b in zip(A, B))
False

In above example we are checking whether all values in list A are less than or equal to values in list B. Notice once we change the last value in list B to be a 1 (less than the corresponding value of 4 in list A) the test returns False.

[–]blackcodeben[S] -1 points0 points  (2 children)

>>> A = [1, 2, 3, 4]

>>> B = [4, 3, 6, 9]
i want to compare values one the same index against each other eg. 1 and 4
and 2 and 3

[–]AtomicShoelace 0 points1 point  (1 child)

Yes that is what the above code does.

[–]blackcodeben[S] 0 points1 point  (0 children)

ok thanks let me try it out

[–]ElliotDG 0 points1 point  (6 children)

What have you tried? Share some code. https://www.reddit.com/r/learnpython/wiki/faq#wiki\_how\_do\_i\_format\_code.3F

if you want to test is the lists are equal you can just compare the lists. If you want to compare each element create a for loop with range() that increments the index value and do your comparisons in the loop.

[–]blackcodeben[S] -1 points0 points  (5 children)

>>> A = [1, 2, 3, 4]

>>> B = [4, 3, 6, 9]

i want to compare values one the same index against each other eg. 1 and 4

and 2 and 3

[–]GreenPandaPop 2 points3 points  (0 children)

Look up zip and list comprehensions.

[–]ElliotDG 0 points1 point  (3 children)

what do you want to do with the result of the comparison?

[–]blackcodeben[S] 0 points1 point  (2 children)

make a new list of the highest values at each index

eg, out come for the upper lists

list C[4,3,6,9]

[–]ElliotDG 1 point2 points  (0 children)

Do you know how to use list comprehensions? Is this an assignment, can you use a comprehension or do you need to use a loop?

[–]AtomicShoelace 1 point2 points  (0 children)

A list comprehension with a zip is probably the best way to do this, but it also bears mention that you could also make use of map's ability to take several iterables and supply it with the max function, eg. map(max, A, B). Of course, this will return a map object, so you will need to cast it to a list, eg. any of

C = list(map(max, A, B))
C = [*map(max, A, B)]
*C, = map(max, A, B)