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 - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Understand set theory with an example45s
-
(Locked)
Find all the elements present in different sets4m 30s
-
(Locked)
Find the common elements present between the sets4m 22s
-
(Locked)
Determine the difference between two sets4m 14s
-
(Locked)
Determine the symmetric difference between two sets2m 6s
-
(Locked)
Determine whether two sets have any common elements1m 14s
-
(Locked)
Determine whether one set is a subset of another1m 41s
-
(Locked)
Determine whether one set is a superset of another2m 36s
-
(Locked)
Solution: List unregistered students1m 20s
-
(Locked)
-
-
-
-
-
-
-