Finding a place in a list by value in Python?

I am kind of new to Python and am considering some new forms of code. I picked 5 random numbers from one to twenty, here in two lists. Like this.

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


Then I select 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, they are in the same position in the list. I have to start with this by following these steps:

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


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


source to share

5 answers

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


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


Allow 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: a solution based on the zip()

one 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 element True

, without unnecessarily listing the rest of the elements.



Change: . It's the same basic idea in a one-line space 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)
>>> foo([1, 2, 69, 3], [3, 4, 69, 5], 3)
>>> foo([1, 2, 2, 3], [3, 4, 2, 5], 2)


Here are some additional tips:

  • Use list

    as a variable name should be avoided as it obscures the inline.
  • No random.randomint

    i think you meantrandom.randint

  • Generating random lists can be done nicely with a list rather than a loop, for example: [random.randint(1, 20) for _ in xrange(5)]



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

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


or more simply using JF 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



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




Python is really simple at this, it's very easy to use. Just use list.index (var), which returns the index that var got in the list.

from random import randint 
list1 = []
list2 = []
for i in range(5):
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'.




All Articles