Python: Intersection of Sets

Post Reply
User avatar
Jason
Site Admin
Posts: 853
Joined: Tue Jul 01, 2025 10:13 am

Code: Select all

first_set_of_words = set((red, building, brown, house, black, jacket))

second_set_of_words = set((red, house, yellow, bee, horse, jacket))

intersection_of_the_two_sets_of_words = first_set_of_words.intersection(second_set_of_words)

# Meaning: The words found in both first_set_of_words and second_set_of_words.

print(intersection_of_the_two_sets_of_words)

# {'red', 'house', 'jacket'} will be displayed
 

POSTREACT(ions) SUMMARY

Post Reply