1
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']
]
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Eskimo868
  • 238
  • 1
  • 12

4 Answers4

2

In both cases, use list comprehension to flatten the list of lists in which you search, thus converting it into an ordinary list (in which you use in to search for name):

name = 'Name 3'

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

if name in [item for sublist in list_1 for item in sublist]:
    print('Name is in the list')

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

if name in [item for sublist in list_1 for item in sublist]:
    print('Name is in the list')

Note that as MisterMiyagi mentioned in the comment, you can also use set comprehension:

if name in {item for sublist in list_1 for item in sublist}:
    print('Name is in the list')

Or you can use generator expression:

if name in (item for sublist in list_1 for item in sublist):
    print('Name is in the list')

These are possibly faster and/or more memory-efficient than list comprehension under some circumstances, such as large lists, or lists with many duplicates.

SEE ALSO:

Generator expressions vs. list comprehensions

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
  • 2
    Heads up that this also works with generator expressions and set expressions instead of list comprehensions. These are more space efficient / possibly faster. – MisterMiyagi Aug 31 '21 at 16:46
  • 1
    @MisterMiyagi Thank you for the great comment, expanded the answer to include your suggestion! – Timur Shtatland Aug 31 '21 at 17:12
  • Thanks! Can I ask how could I check if the name is not in the list, do I need to only change the "if name in" part? – Eskimo868 Aug 31 '21 at 18:30
  • @Eskimo868 Yes, to check if the name is **not** in the list, just use `if not name in ...` in all cases. Or use `if name not in ...`. – Timur Shtatland Aug 31 '21 at 18:34
1

Use

if Name in Name_list[0]:
    print(Name)

The Name_list[0] is because, you are creating a list inside a list. So, Name_list contains a list inside it. If you want to access the list inside Name_list, you will have to use the list index operator.

Sarvesh M.D
  • 192
  • 1
  • 11
1

Use any

if any(Name in sublist for sublist in Name_list):
    print('Name is in the list')
0
Name = ''
Name_list = []
Name_list_v2 = []
if len(Name_list)==1:
    Name_list_v2=Name_list[0]
elif len(Name_list)>1:
    for names in Name_list:
        Name_list_v2.append(names[0])
else:
    print("Input formatted incorrectly")
for names in Name_list_v2:
    if names == Name:
        print("Name is in the list.")
    else:
        print("Name is not in the list")

Line by line description of what happens:

  • line 1 sets the Name variable
  • line 2 is where you input the list of names (you can format it as either [['Name 1', 'Name 2', 'Name 3']] or [['Name 1'], ['Name 2'], ['Name 3']] (you don't have to use three variables)
  • line 3 creates Name_list_v2, which will eventually be formatted as ['Name 1', 'Name 2', 'Name 3'] regardless of which way you chose to input it
  • lines 4&5 check if the list is formatted as [['Name 1', Name 2', 'Name 3']], and if it is converts it into ['Name 1', 'Name 2', 'Name 3']
  • lines 6-8 check if the list is formatted as [['Name 1'], ['Name 2'], ['Name 3']], and if it is, change it to ['Name 1', 'Name 2', 'Name 3']
  • lines 9&10 print an error message if the input was formatted wrong
  • lines 11-15 check if the name you inputted on line 1 is in Names_list_v2, and print a response.