4

What would be the best way to ensure that two different variables are in the same list before doing something with them? For example:

var1 = 1
var2 = 2
var3 = 3
var4 = 4
list1 = [var1, var2, var3, var4]
var5 = 5
var6 = 6
var7 = 7
var8 = 8
list2 = [var5, var6, var7, var8]

if var1 and var2 (are in the same list):
    print("In the same list")
else: 
    print("Not the same")

Would it be better to use a tuple or something else instead of lists, which would make this easier?

tshepang
  • 12,111
  • 21
  • 91
  • 136
John Smith
  • 63
  • 2
  • 4
  • 1
    By variables being in the same list, do you mean their values? – bereal Sep 07 '13 at 19:07
  • This sounds like you're unfamiliar with how to best structure your data, and you've hit upon the wrong solution to some other problem you were trying to solve. Can you describe the problem you're hoping to solve with this? – user2357112 Sep 07 '13 at 19:33

3 Answers3

4
>>> list1 = list(range(1, 5))
>>> 1 in list1 and 2 in list1
True
>>> 1 in list1 and 6 in list1
False

If you're checking multiple items, then go for all:

>>> to_be_checked = [1, 2, 3, 4]
>>> all(item in list1 for item in to_be_checked)
True

Don't create separate variables for each item, just put them in the list from the start.


For efficieny use sets, sets provide O(1) loopkup. Note that sets don't have any order and you can't do indexing on them

>>> list1 = list(range(10**5))
>>> s = set(range(10**5))
>>> %timeit 1000 in list1 and 10**5 in list1
100 loops, best of 3: 2.71 ms per loop
>>> %timeit 1000 in s and 10**5 in s
1000000 loops, best of 3: 403 ns per loop
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
3

The cleanest way to do this I think is to test whether your var_1 and var_2 are a subset of one or more of your lists

list_a = [1,2,3,4]
list_b = [5,6,7,8]
test_list = [1,4]    
if set(test_list)issubset(set(list_a)):
    do something

this preserves your data in their list form so you can do what you like with them and it is fast and not as confusing as using the and statement - which in my opinion adds unnecessary complexity to code

Just to be clear I am asking Python to test whether the set that has as its members the members of the test_list is a subset of the set that has as its members the members of list_a

Actually I think you are asking something more specific here - so you have some lists- presumably you have some way to get the lists to you can iterate for them

 my_lists = [ [1,2,3,4], [5,6,7,8]]

 temp_list = [1,4]

 for each_list in my_lists:
     if set(temp_list).issubset(set(each_list)):
         print 'members of the same list'
      else:
         print 'not members of the same list'
PyNEwbie
  • 4,882
  • 4
  • 38
  • 86
0

On the face of it:

if ((var1 in list1 and var2 in list2) or (var1 in list2 and var2 in list2))

to generalize a bit:

mylists = (list1, list2)
if any(var1 in x and var2 in x for x in mylists)

to generalize a bit more:

mylists = (list1, list2)
myvars = (var1, var2)
if any(all(v in x for v in myvars) for x in mylists)

Or you could look at it totally differently:

which_list = dict()
which_list.update((val, 1) for val in list1)
which_list.update((val, 2) for val in list2)

if which_list[var1] == which_list[var2]

of course, that assumes each variable appears in exactly one list.

Note that in all cases, you aren't really checking whether the variable is in the list. A variable cannot be in a list. You're checking whether the current value of the variable is in the list. So if any of the variables refer to identical values, then there's no way to tell from the list which variable name was used to put the value in the list.

As Ashwini Chaudhary says, a set can generally execute in faster than a list or tuple. But for such small lists it's unlikely to make a noticeable difference.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699