0

Is it possible to check if something is in a list of lists at a specific index? Example:

l_list = [['a', 2, True], ['b', 1, False], ['c', 5, True], ...]
if 'a' in l_list:
    print('Yes')

I want to check if 'a' exists inside of l_list (and just ignore other values) as any of the sub-lists first value without iterating over the whole list.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Qaimaq
  • 117
  • 7
  • 1
    If the thing you are looking for is always the first thing in the sublist you could use: `if 'a' in (first for first, *rest in l_list):`, but you need to look at the sublists up to the point where you find (or don't find) a match. – Mark Oct 02 '21 at 17:09
  • 5
    Anything you do here is going to be morally equivalent to a loop. The real best answer for how to accomplish what you want depends on what you are actually trying to accomplish and the real structure of your data. – Tim Seguine Oct 02 '21 at 17:09
  • `'a'` is NOT in `l_list`. It is inside a list that is a an entry in `l_list`. What do you want to check? – balderman Oct 02 '21 at 17:13
  • @balderman they are trying to check `any(('a' in l for l in l_list))` – Tim Seguine Oct 02 '21 at 17:15
  • 1
    Does this answer your question? [Check if a sublist contains an item](https://stackoverflow.com/questions/13728023/check-if-a-sublist-contains-an-item) – NickS1 Oct 02 '21 at 17:27
  • @NickS1 In that question, the top-level list contains non-lists, and OP doesn't specify that the element must be at a certain position. – wjandrea Oct 02 '21 at 17:35
  • I'm so sorry for that, I should've read with more attention! But now I see the difference! Thank you for correcting me! – NickS1 Oct 02 '21 at 20:18

3 Answers3

2

Loop over the sublists (for sub in l_list), access the first value (sub[0]), and check if it's 'a':

if any(sub[0]=='a' for sub in l_list):
    print('Yes')

Using any() means it'll stop checking once it finds 'a'.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    I like this one the best because it is almost a literal translation of the question into code, and it should also be among the most efficient without literally writing a `for` loop. – Tim Seguine Oct 02 '21 at 20:10
1

you can iterate over the first elements:

>>> 'a' in [item[0] for item in l_list]
True

but that's still O(n)

Hadar
  • 658
  • 4
  • 17
  • 8
    If you make this a generator rather than a list, you can at least exit early if the match is found: `(item[0] for item in l_list)`. – Mark Oct 02 '21 at 17:12
0

The following code will recursively check thru each sub table for the element no matter how dimensional your table is:

def find_reccursive(table, item_to_find):
    for el in table:
        if isinstance(el, list):
            return find_reccursive(el, item_to_find)
        else:
            return item_to_find in table
                
l_list = [['a', 2, True], ['b', 1, False], ['c', 5, True], ...]
if find_reccursive(l_list, 'a'):
    print("Found a")
David Hožič
  • 235
  • 2
  • 7