I have a code:
dates = []
dates.append(['imie', 'nazwisko'])
dates.append(['test', 'test2'])
How can check if "imie" and "nazwisko" is in dates?
if 'imie' and 'nazwisko' in dates - not work
Thanks for help
falsetru
371k69 gold badges770 silver badges660 bronze badges
2 Answers 2
Using any and generator expression:
>>> dates = []
>>> dates.append(['imie', 'nazwisko'])
>>> dates.append(['test', 'test2'])
>>> any(('imie' in d and 'nazwisko' in d) for d in dates)
True
UPDATE
You can also use set.issubset as suggested by Jon Clements:
>>> any({'imie', 'nazwisko'}.issubset(d) for d in dates)
True
answered Jan 28, 2015 at 14:35
falsetru
371k69 gold badges770 silver badges660 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
falsetru
@JonClements, Thank you for comment. I added it to the answer according to you.
Well you're appending lists to dates instead of the strings, so:
for i in dates:
if 'imie' in i or 'nazwisko' in i:
return True
return False
answered Jan 28, 2015 at 14:33
Benjamin James Drury
2,3661 gold badge18 silver badges27 bronze badges
Comments
lang-py
dates?setsand their operators: docs.python.org/2/library/sets.html