fbpx

Sets

In Python, a set is a collection of unique and unordered elements, which can be of different data types. Sets are typically created by using the built-in set() function. Sets are mutable, which means that you can add or remove elements after they are created. Sets are nearly identical to lists, except for the fact that they cannot contain duplicate elements, are notated with curly braces { } instead of hard brackets [ ].

For example, here is a practical application of sets:

				
					# create a list:
employeeOfTheMonth = ["John", "John", "Maria", "Carlos", "Maria", "Anne"]

# remove duplicates from the list using the set() function:
employeeOfTheMonthNoDuplicates = set(employeeOfTheMonth)

print(employeeOfTheMonthNoDuplicates)
# {"John", "Maria", "Carlos", "Anne"}
				
			

Replit Practice

Loading...