From the course: Python Data Structures: Sets and Frozen Sets

Unlock this course with a free trial

Join today to access over 24,100 courses taught by industry experts.

Find all the elements present in different sets

Find all the elements present in different sets - Python Tutorial

From the course: Python Data Structures: Sets and Frozen Sets

Find all the elements present in different sets

- [Instructor] On line one, two, three. We have defined three sets, group1, group2, and group3. How would you calculate all the unique colors of Lego blocks available to you? Now before we use any built-in method, let us first see how we can approach the problem without using any of the built-in methods. One of the possible approaches is we initialize an empty set named as unique_colors and then iterate over all the available sets. So for color in group1: unique_colors.add(color) for color in group2: unique_colors.add(color) for color in group3: unique_colors.add(color) and then we can print the result. print("All unique colors available") "All unique lego block colors available are " let us print the unique_color and then also calculate the count. ("Count of unique color blocks is ", len(unique_colors)) len is the built-in method to calculate length. And now we can print the result to see the output. python3 union.py And here is the result. All unique lego block colors are, these are…

Contents