'How to search for a string inside another list? - python

I need to search for a name in a list.

When I do it this way I have no problem:

#contacts = [['1','Juan Corrales','Loyer','Peru'],['2','Felix Tadeo','Police','Argentina']]
    contacts = ['Juan Corrales','FELIX']
    name = 'Juan Corrales'
    
    if name in contacts:
        print("name found")
    else:
        print("name not found")

But when I look for it inside a double list, it doesn't find it.

How could I solve this?

contacts = [['1','Juan Corrales','Loyer','Peru'],['2','Felix Tadeo','Police','Argentina']]
#contacts = ['Juan Corrales','Felix Tadeo']
name = 'Juan Corrales'

if name in contacts:
    print("name found")
else:
    print("name not found")


Solution 1:[1]

You can use the any function:

contacts = [['1','Juan Corrales','Loyer','Peru'],['2','Felix 
Tadeo','Police','Argentina']]
#contacts = ['Juan Corrales','Felix Tadeo']
name = 'Juan Corrales'

print("name found") if any(name in contact for contact in contacts) else print("name not found")

Most concise, no dependencies required

Solution 2:[2]

That's because contacts contains lists as elements, and not strings. Something you can do is to flatten the contacts list, and then search there:

contacts = [['1','Juan Corrales','Loyer','Peru'],['2','Felix Tadeo','Police','Argentina']]
#contacts = ['Juan Corrales','Felix Tadeo']
flat_contacts = [item for sublist in contacts for item in sublist]
name = 'Juan Corrales'

if name in flat_contacts:
    print("name found")
else:
    print("name not found")

Solution 3:[3]

You need to flatten the list back to 1 dimension. You can do it multiple ways:

# For when there are only 2 sublists
if name in contacts[0]+contacts[1]:
# When there are n sublists
if name in [i for t in contacts for i in t]:

The in operator only looks at the surface, it does not scan through all nested sublists. Consider the example:

>>> a = [1, 2, 3, 4]
>>> b = [[1, 2], [3, 4]]

>>> 1 in a
True
>>> 1 in b
False
>>> [1, 2] in b
True

Solution 4:[4]


import itertools
contacts = [['1','Juan Corrales','Loyer','Peru'],['2','Felix Tadeo','Police','Argentina']]
name = 'Juan Corrales'

if {name}.issubset(itertools.chain.from_iterable(contacts)):
    print("Name Found")
else:
    print("Name not Found")

output:

Name Found

Solution 5:[5]

contacts = [['1','Juan Corrales','Loyer','Peru'],['2','Felix Tadeo','Police','Argentina']]
name = 'Juan Corrales'
found = any([True for c in contacts if name in c]) #True

Just iterating over lists inside the contact list and then checking inside of them - if found in any one of them, voila!!! we got it.

Solution 6:[6]

Your if statement is looking for a string in the contacts list, but your contacts list contains multiple lists. One way would be to use itertools to join inner lists and look for your value here. Here is an example code:

import itertools

contacts = [['1','Juan Corrales','Loyer','Peru'],['2','Felix Tadeo','Police','Argentina']]
#contacts = ['Juan Corrales','Felix Tadeo']
name = 'Juan Corrales'

if name in list(itertools.chain.from_iterable(contacts)):
    print("name found")
else:
    print("name not found")

Just have in mind, that it will only work for you if you have a list of lists.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 thpbaxxter
Solution 2 idanz
Solution 3 Freddy Mcloughlan
Solution 4 Raj Patel
Solution 5 user2736738
Solution 6 DharmanBot