0

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

2
  • 1
    You need to check that both of them are present in dates? Commented Jan 28, 2015 at 14:28
  • You can use sets and their operators: docs.python.org/2/library/sets.html Commented Jan 28, 2015 at 14:46

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

@JonClements, Thank you for comment. I added it to the answer according to you.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.