0 votes
1 view
in Python by (40.4k 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 (92.9k 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
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...