pythonlistlist-comprehensiongenerator-expressionset-comprehension

How to compare a string to any object in the list?


Name_list = [
    ['Name 1', 'Name 2', 'Name 3']
]

Name = 'Name 3'

if Name == 'any name in the list':
    print('Name is in the list')

How can I check if "Name 3" is in the list?

And also when the list looks like this:

    list_1 = [
    ['Name 1'],
    ['Name 2'],
    ['Name 3']
]

Solution

  • Use any

    if any(Name in sublist for sublist in Name_list):
        print('Name is in the list')