w3resource

Python: Find common items from two lists

Python List: Exercise - 37 with Solution

Write a Python program to find common items in two lists.

Python: Find common items from two lists

Sample Solution:

Python Code:

# Define a tuple 'color1' containing color names
color1 = "Red", "Green", "Orange", "White"

# Define another tuple 'color2' containing color names
color2 = "Black", "Green", "White", "Pink"

# Create sets from 'color1' and 'color2' to perform set intersection (common elements)
# Print the result, which contains the colors that are common to both sets
print(set(color1) & set(color2))

Sample Output:

{'Green', 'White'} 

Flowchart:

Flowchart: Find common items from two lists

Python Code Editor:

Previous: Write a Python program to get variable unique identification number or string.
Next: Write a Python program to change the position of every n-th value with the (n+1)th in a list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.