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)

7
  • What exactly do you mean by "go into that list"? Commented Oct 2, 2014 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. Commented Oct 2, 2014 at 18:59
  • Your question is answered here: stackoverflow.com/a/1156143/1628832 Commented Oct 2, 2014 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). Commented Oct 2, 2014 at 19:03
  • What types are the elements of the lists? Numbers, strings, objects, mixed? Commented Oct 2, 2014 at 19:12

4 Answers 4

4

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]
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Thank you. I might just need to use pandas instead to join on the column I find my elements in...
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)

7 Comments

is there any other way to do this than looping over all the lists? i have 2000 lists, each with 8 elements in them.
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.
@user3264659: What's the issue with looping over 2000 lists?
@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.
@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.
|
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

Comments

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

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.