mardi 3 août 2021

Using any() in condition to find value inside tuple, inside list [duplicate]

I want to validate if a certain value is present within a tuple inside a list.

Now I wonder why this works:

list = [(1,2)] # tuple inside a list
# check if numbers are in tuple
if any(3 in pair for pair in list) or any(4 in pair for pair in list):
    print('Yep, found something')
else:
    print('Nope')
output: 'Nope'

And why this doesn't work:

list = [(1,2)] # tuple inside a list
# check if numbers are in tuple
if (3 in pair for pair in list) or (4 in pair for pair in list):
    print('Yep, found something')
else:
    print('Nope')
output: 'Yep, found something'

What are the mechanics at play here? Why doesn't example 2 work?

I clearly misunderstood something, but cannot find out. Can anyone explain please?

Aucun commentaire:

Enregistrer un commentaire