1

for example,

listOne = [1,2,6]
listTwo = [3,2,4]

checkLists(listOne, listTwo)

should return [3,2,6]

1 < 3 therefore [3,?,?]

2 = 2 therefore [3,2,?]

6 > 4 therefore [3,2,6]

I have recently started python and don't know how to use a loop that checks two lists at once.

wa7d
  • 129
  • 1
  • 2
  • 12

4 Answers4

7

You can use a list comprehension to make a new list.
You can use zip to iterate through both lists at once.
You can use max to give you the greater of two items.

def checkLists(a, b):
    return [max(ai, bi) for ai, bi in zip(a,b)]

This gives:

>>> checkLists([1,2,6], [3,2,4])
[3, 2, 6]
khelwood
  • 55,782
  • 14
  • 81
  • 108
4

You can use the map built-in function here:

result_iter = map(max, list_one, list_two)

which will create a list on Python 2 and an iterator (a map object) on Python 3 - if you do need a list, on Python 3 wrap the map with list():

result_list = list(map(max, list_one, list_two))

Example:

>>> list_one = [1, 2, 6]
>>> list_two = [3, 2, 4]
>>> list(map(max, list_one, list_two))
[3, 2, 6]

How this works is that the map function takes 1 function as an argument, followed by 1 to n iterables; the iterables are iterated over simultaneously and their values are passed as arguments to the given function; whatever is returned from the function (max in this case) is yielded from the map into the result value.

  • I didn't realise that `map` was so fast! For small lists, a `map` based function runs in 2/3 the time of a function containing a list comprehension using `zip` (in Python 2); for a list of length 1000, the `map` version runs in less than half the time of the list comp. And because this version is so compact there's little point in wrapping it in a function, and of course calling it directly would be even faster. – PM 2Ring Apr 21 '15 at 10:33
  • 1
    This is fast because in Python resolving global names is slow; with list comprehension or generator expression you pay the price of finding out "*what's this `max`?*" 1000 times where the `map` does it once. Furthermore `map` does not need to rebuild tuples twice in this case. – Antti Haapala -- Слава Україні Apr 21 '15 at 12:44
3
[max(x,y) for x,y in zip(listOne, listTwo)]
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0
listOne = [1,2,6]
listTwo = [3,2,4]

import itertools
def checkLists(listOne, listTwo):
    lsttmp=[]
    for i , j in itertools.izip(listOne,listTwo):

        lsttmp.append(max(i,j))
    return  lsttmp
print checkLists(listOne, listTwo)
Himanshu dua
  • 2,496
  • 1
  • 20
  • 27
  • Why import `.izip` when you can use the built-in `zip` for this? OTOH, `.izip` creates an iterator whereas `zip` creates a list, so `.izip` can be better if the input lists are very large. – PM 2Ring Apr 21 '15 at 07:40
  • @PM2Ring zip computes all the list at once, izip computes the elements only when requested.So i have used the izip – Himanshu dua Apr 21 '15 at 07:49
  • Ok. I just did some timing tests. `izip` and `zip` are roughly the same speed for very small lists, but for larger lists (>100 elements) `izip` is a little faster. However, using a list comprehension makes more of a difference: a list comp using `izip` on lists of 1000 elements runs in 2/3 the time of `zip` in a `for... append` loop. – PM 2Ring Apr 21 '15 at 08:34