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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/list/python-data-type-list-exercise-37.php