Skip to content

Latest commit

 

History

History
44 lines (27 loc) · 1.5 KB

File metadata and controls

44 lines (27 loc) · 1.5 KB

Sets

Examples

countries = ['Angola', 'Maldives', 'India', 'United States', 'India', 'Denmark', 'Sweden', 'Ghana', ... 777 more countries not displayed]

print(len(countries)) # 785
print(countries[:5]) # ['Angola', 'Maldives', 'India', 'United States', 'India']

country_set = set(countries)
print(country_set) = 196

country_set.add('Italy')

A set is a data type for mutable unordered collections of unique elements. One application of a set is to quickly remove duplicates from a list.

numbers = [1, 2, 6, 3, 1, 1, 6] unique_nums = set(numbers) print(unique_nums)

This would output:

{1, 2, 3, 6}

Sets support the in operator the same as lists do. You can add elements to sets using the add method, and remove elements using the pop method, similar to lists. Although, when you pop an element from a set, a random element is removed. Remember that sets, unlike lists, are unordered so there is no "last element".

fruit = {"apple", "banana", "orange", "grapefruit"} # define a set

print("watermelon" in fruit) # check for element

fruit.add("watermelon") # add an element print(fruit)

print(fruit.pop()) # remove a random element print(fruit)

This outputs:

False {'grapefruit', 'orange', 'watermelon', 'banana', 'apple'} grapefruit {'orange', 'watermelon', 'banana', 'apple'}

Other operations you can perform with sets include those of mathematical sets. Methods like union, intersection, and difference are easy to perform with sets, and are much faster than such operators with other containers.