'Finding place in list according to value in Python?

I am looking at some new code forms. I have chosen 5 random numbers between one and twenty, here, in two lists. Like so.

list = []
listn = []
import random
for i in range(5):
     newvar = random.randomint(1,20)
     list.append(newvar)
     newvart = random.randomint(1,20)
     listn.append(newvart)

Then I choose another variable in the same code.

evar = random.randomint(1,20)

What I want to do is see if the number is in both lists, and if so, if they are at the same position in the list. I should begin this by doing the following:

if (evar in list) and (evar in listn):

But I don't know how to do the rest. I want to find out if evar is in both lists and is at the same position in both lists (i.e. it is the third number in list and listn). How would I do this?



Solution 1:[1]

Assuming the first found positions should be the same using list.index() method:

def f(lst1, lst2, value):
    try: return lst1.index(value) == lst2.index(value)
    except ValueError:
        return False

Allowing all positions using set intersection:

def positions(lst, value):
    return (pos for pos, x in enumerate(lst) if x == value)

def f(lst1, lst2, value):
    return bool(set(positions(lst1, value)).intersection(positions(lst2, value)))

Or even better: zip()-based solution suggested by @wim:

from itertools import izip

def f(lst1, lst2, value):
    return any(x1 == x2 == value for x1, x2 in izip(lst1, lst2))

Note: any() returns as soon as it finds the first True item without needlessly enumerating the rest of the items.

Solution 2:[2]

Edit: This is the same basic idea in a one-liner, as posted by JF in the comments below:

any(x1 == x2 == evar for x1, x2 in zip(list1, list2))


>>> def foo(list1, list2, evar):
...   for x1, x2 in zip(list1, list2):
...     if x1 == x2 == evar:
...       return True
...   else:
...     return False
... 
>>> foo([1, 2, 69, 3], [3, 4, 69, 5], 69)
True
>>> foo([1, 2, 69, 3], [3, 4, 69, 5], 3)
False
>>> foo([1, 2, 2, 3], [3, 4, 2, 5], 2)
True

Here are a couple of extra tips:

  • Using list as a variable name should be avoided because it shadows the built-in.
  • There is no random.randomint, I guess you meant random.randint
  • Generating the random lists can be done nicely with a list comprehension rather than a loop, like this: [random.randint(1, 20) for _ in xrange(5)]

Solution 3:[3]

If you need to know the index where the match occurs, you can use something like this

try:
    idx = next(i for i,x in enumerate(list1) if evar==x==list2[i])
    ...
except StopIteration:
    # not found
    ...

or more simply using J.F. Sebastian's suggestion

idx = next((i for i,x in enumerate(list1) if evar==x==list2[i]), -1)

will return -1 if there is no match

Solution 4:[4]

if (evar in list):
    if (evar2 in listn):
        if (evar == evar2 and list.index(evar2) == listn.index(evar2):
            do_something()

Solution 5:[5]

Python is really easy in these ways, it's extremely easy to handle. Simply use the list.index(var) which returns the index that var got in list.

from random import randint 
list1 = []
list2 = []
for i in range(5):
    list1.append(randint(1,20))
    list2.append(randint(1,20))
evan = randint(1,20) 
if (evan in list1 and evan in list 2) and (list1.index(evan) == list2.index(evan)):
    print 'They are at the same place'.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2
Solution 3
Solution 4 dwerner
Solution 5 Martol1ni