0

If I have something like:

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

Then how do I check if 1 is in the first, second, or third array?

I want it to be able to make an expression such as:

if 1 is in list 1:

   do something

elif i is in list 2:

   do something

else: 

   do something
1
  • What problem are you trying to solve with this code? This seems like an XY problem Commented Apr 5, 2016 at 5:12

6 Answers 6

2

Try using any:

any(1 in sub for sub in [[1,2,3],[4,5,6],[7,8,9]])

>>> list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> any(1 in sub for sub in list)
True
>>> any(10 in sub for sub in list)
False
>>> any(7 in sub for sub in list)
True
>>> 
Sign up to request clarification or add additional context in comments.

Comments

0

Well, if you know the contents of the list, you can use

if 1 in list[0]:
    do_something()
elif 1 in list[1]:
    do_something_else()
else: # 1 is in list[2]
    do_something_different()

Comments

0

Derived from "Dive Into Python"

list = [[1,2,3],[4,5,6],[7,8,9]]
if 1 in list[0]:

   do something

elif 1 in list[1]:

   do something

else:

   do something

...and so on. The in keyword takes a preceding value argument and returns a true if that value is in the proceeding list, false otherwise. The only other thing to know is list[0] accesses your first element in the top level list (the first sublist) and so on, allowing it to be searched for a specific integer using the in keyword.

Here's the expected output:

>>> list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> 1 in list[0]
True
>>> 1 in list[1]
False

To search all lists you can use the for keyword to loop over your lists and any to return true if any of the sublists match.

>>> any(1 in sublist for sublist in list)
True
>>> any(42 in sublist for sublist in list)
False

Comments

0

You can use Python's built-in "in" operator. This operator returns True if the item is in the iterable (list in this case), or False if it is not.

x_in_array = x in array

The Python doc that covers this is here, under section 5.6: https://docs.python.org/2/library/stdtypes.html

Your if/elif/else statements would then look like:

if x in array_1:
    # do something
elif x in array_2:
    # do something
else:
    # do something else

Comments

0

If you want to return the index of the list:

>>> def idx_check(i, l):
...     for (idx, val) in enumerate(l):
...         if i in val:
...             return idx
...     return None
...

>>> l = [[1,2,3],[4,5,6],[7,8,9]] 
>>> idx_check(1, l)
0
>>> idx_check(4, l)
1
>>> idx_check(7, l)
2

Comments

0

First of all: I know it's just an example, but don't use list as a variable in Python. It is a keyword/built-in function.

Second: you can use iteration to accomplish this. You can iterate through the members of a list (or any iterable object, or any iterator) like this:

for item in L:
    # do something with item

You can keep track of the number of times you have entered the loop using the enumerate function. The enumerate function returns a two-tuple in the form of member_index_number, member:

for num,item in enumerate(L):
    # do something with num and item

Almost there- now you need a way to associate the above iteration with your list of functions. The usual way this is done in python is with a dictionary, but you could also just use a list.

# list way
funcs_list = [f1, f2, f3]
for num,item in enumerate(L):
    if 1 in item:
        funcs_list[funcs_list.index(num)]()
        break

# dictionary way
funcs_dict = {0: f1, 1: f2, 2: f3}
for num,item in enumerate(L):
    if 1 in item:
        funcs_dict[num]()
        break

The dictionary method is usually preferred because it looks much cleaner.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.