5

I have a list of lists that correspond to lines in file, with multiple columns.

[[col1, col2, col3], [elem1, elem2, elem3], [elem4, elem5, elem6]] 

I want to check if (for example) elem3 is in any of the lists, and if it is, go into that list. (really I have a list of things I need to check, so it's a list that probably contains elem3, elem5, elem7.... etc)

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
user3264659
  • 341
  • 2
  • 7
  • 20
  • What exactly do you mean by "go into that list"? – NPE Oct 02 '14 at 18:52
  • just return it (i'm planning on evaluating the other elements in the list). It's as if I'm using the elem3 as a key into the list. unfortunately the file is not set up as a dictionary. – user3264659 Oct 02 '14 at 18:59
  • Your question is answered here: http://stackoverflow.com/a/1156143/1628832 – karthikr Oct 02 '14 at 19:00
  • @karthikr: I don't believe that answer applies to this question (it returns a boolean instead of returning the matching list, and can't be easily adapted to return the list). – NPE Oct 02 '14 at 19:03
  • What types are the elements of the lists? Numbers, strings, objects, mixed? – Roger Fan Oct 02 '14 at 19:12
  • the elements of the lists are ints (it could be a string though, i don't know how csv's are read in, if there's anything converted) – user3264659 Oct 03 '14 at 22:22
  • Does this answer your question? [Check if element exists in tuple of tuples](https://stackoverflow.com/questions/15124833/check-if-element-exists-in-tuple-of-tuples) – Georgy Oct 17 '20 at 10:05

4 Answers4

3

the shortest way is to use list comprehensions and list comprehensions sometimes are more faster than simple for loop

your list:

list1 =  [ ["col1", "col2", "col3"], ["elem1", "elem2", "elem3"], ["elem4", "elem5", "elem6"] ]

your element to find:

to_find = "col1"

your function to "go into that list":

def do_something(sub_list):
    print (sub_list)

and the list comprehension that will find your element and call function with list that have it:

[do_something(sub_list) for sub_list in list1 if to_find in sub_list]
pasichnyk.oleh
  • 222
  • 3
  • 8
  • 1
    Note that this doesn't actually avoid the nested loops problem of @BobbyRussell's answer. It just hides it. List comprehensions have essentially the same performance as for loops. – Roger Fan Oct 02 '14 at 19:11
  • Thank you. I might just need to use pandas instead to join on the column I find my elements in... – user3264659 Oct 03 '14 at 22:24
1

You can do something like this:

def in_list(list_of_lists, item):
    for list_ in list_of_lists:
        if item in list_:
            return list_

EDIT:

Here is a recursive version for the heck of it:

def in_list(list_of_lists, item):
    if not list_of_lists:
        return None
    if item in list_of_lists[0]:
        return list_of_lists[0]
    return in_list(list_of_lists[1:], item)
Bobby Russell
  • 475
  • 2
  • 12
  • is there any other way to do this than looping over all the lists? i have 2000 lists, each with 8 elements in them. – user3264659 Oct 02 '14 at 18:58
  • The complexity of this algorithm is not something you should be worried about. `in` doesn't iterate over the list, so you're just going to iterate over the list_of_lists. It should not be slow at all. – Bobby Russell Oct 02 '14 at 19:00
  • @user3264659: What's the issue with looping over 2000 lists? – NPE Oct 02 '14 at 19:00
  • 5
    @BobbyRussell *"`in` doesn't iterate over the list"* Yes it does. `item in lst` is a `O(n)` operation that iterates over `lst` until `item` is found, or `lst` is exhausted. – dano Oct 02 '14 at 19:01
  • @Leistungsabfall If you only want the first occurrence of `item` in one of the sub-lists, you wouldn't want a list comprehension here, because you can't short-circuit it as soon as you find a match. With this answer, you stop as soon as `item` is found. – dano Oct 02 '14 at 19:08
  • @Leistungsabfall Your answer is not what the poster is looking for... that comprehension returns a list of lists. – Bobby Russell Oct 02 '14 at 19:10
  • @BobbyRussell My bad! +1 for your answer. – Leistungsabfall Oct 02 '14 at 19:16
0

I came across a similar problem. I had a list of tuples, with tile numbers:

data = [(0, 0, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 0),
    (0, 23, 24, 25, 26, 27, 28, 29, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 0),
    (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)]

I needed to find out where in the list of tuples a particular tile number is located, so:

to_find = 42
for i in range(len(data)):
    if to_find in data[i]:
        print (i, data[i].index(to_find))
        break
Dobedani
  • 508
  • 5
  • 20
0

You can check if a value exists in a list of lists as shown below:

list_of_lists = [['John', 'David'], ['Anna', 'Lisa']] 

print(any('Lisa' in list for list in list_of_lists)) # True
print(any('Tom' in list for list in list_of_lists)) # False
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129