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")
CodePudding user response:
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
CodePudding user response:
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")
CodePudding user response:
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
CodePudding user response:
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
CodePudding user response:
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.
CodePudding user response:
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.