Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I want to compare 2 iterables and print the items which appear in both iterables.

>>> a = ('q', 'r') 

>>> b = ('q') 

# Iterate over a. If y not in b, print y. 

# I want to see ['r'] printed. 

>>> print([ y if y not in b for y in a]) 

^

But it gives me an invalid syntax error where the ^ has been placed. What is wrong about this lambda function?

1 Answer

0 votes
by (106k points)

The code that you have written is in wrong order wrong. The if should be after the for (unless it is in an if-else ternary operator)

[y for y in a if y not in b]

Related questions

0 votes
2 answers
asked Sep 17, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...