Set

Set is an unordered collection of data items that stores multiple items in a single variable.

Sets are:

  1. Unchangeable - we can not change items of a set.
  2. Unordered - items in a set are unordered.
  3. No index - set items do not have indexes.
  4. Unique - items of a set are unique.

Whatever input you give will be stored in variable "name" and we can use this variable when we have to print the name.

We can also perform some mathematical operation on our input. By default our input has string data type and for doing mathematical operations, we have to convert it into number. We will discus this in string section.

Unordered and unindexed

Items in a set are unordered and do not have specified index. When you compile your code multiple times, order of items may change. Items are not at the same index every time.

Unchangeable

Items in a set are unchangeable. Once you create a set, you can not update its values. However you can add or remove items from a set. You can check add and remove methods in method section.

Duplicates

Duplicate items are not allowed in a set. If you add duplicate items in a set, duplicates will be discarded.

Length of Set

We can calculate the length of sets using length method. Length of a set is equal to the number of unique items in a set. The duplicate items are counted once.

Data types of items of Sets

Data type of items of a set may or may not be same. Sets can store data with items having different data types.

Accessing items in a Set

We can not access items in list using indexes. We have to use loop to access items in a set.

Using "in" and "not in" to check presence of items:

We can not access items in list using indexes. We have to use loop to access items in a set.

Joining two sets

We can join two sets using union and duplicate methods. If two sets have repeated items, they will exclude duplicate items.

Set Methods

OperatorCodeDescription
.add()cars.add("Volvo")Adds a single element to a set.
.clear()numbers.clear()Removes all the elements from a set.
.copy()copied_cars = cars.copy()Creates shallow copy of orignal set.
.difference()difference = set1.difference(set2)Finds the difference between two sets.
.difference_update()set1.difference_update(set2)Updates the calling set by removing elements that are also present in one or more specified sets.
.discard()nums.discard(3)Removes a specified element from the set if it exists.
.intersection()car = cars.intersection(fav_cars)Finds the intersection of two or more sets.
.intersection_update()animals.intersection_update(fav_animals)Updates the calling set by keeping only the elements that are also present in one or more specified sets.
.isdisjoint()result = set1.isdisjoint(set2)Checks if two sets have no common elements.
.issubset()result = set1.issubset(set2)Checks whether one set is a subset of another set.
.issuperset()result = set1.issuperset(set2)Checks whether one set is a superset of another set.
.pop()removed_element = nums.pop()Removes and return an arbitrary (random) element from the set.
.remove()nums.remove(3)Removes a specific element from the set.
.symmetric_difference()symmetric_diff = set1.symmetric_difference(set2)Finds the symmetric difference between two sets and returns a new set.
.symmetric_difference_update()set1.symmetric_difference_update(set2)Updates the calling set with the symmetric difference between the calling set and another set.
.union()union_set = set1.union(set2)Finds the union of two or more sets
.update()set1.update(set2)Updates the calling set by adding elements from another set or an iterable (like a list, tuple, or another set).

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which of the following is a best practice in programming?

10 XP~2 min
Exercise 2 of 2Easy

Code that is easy to read and understand is called ___ code.

10 XP~2 min