Set basics
A set is a collection of elements that are unique and unordered.
Create a set with the set() function, which accepts a sequence-type object that is iterable such as a list, tuple, or string. Iterable means that it has multiple elements that you can access one at a time, like items in a list. The elements are inserted into the set with the set() function. A set literal uses curly braces { } with commas separating set elements. The set() function does not allow empty sets to be created.
Create a set using the set() function.
nums1 = set([1, 2, 3])
Create a set using a set literal.
nums2 = { ‘A’, ‘B’, ‘C’ }
Print the contents of the sets.
print(nums1)
print(nums2)
Since sets are unordered, that means they don’t have indexes for each element. So trying to access a set element my_set[2] will cause a runtime error.
Use a set if you want to remove duplicates from a list, so that you only have a collection of unique values. You can do that by just passing your list object into the set() method, and that will remove all the dupes and create the new set.
Sets are mutable and you can call on some functions to change them. The following lists the functions and methods available to update your set.
Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!