Unordered Collection

Python provides two data structures for unordered collection: Dictionaries and Sets.


Dictionary is an unordered set of key:value pairs in which the keys are unique. A pair of braces creates an empty dictionary: { }

Here is an example of dictionary with values:

country_codes = {'US':'United States', 'UK':'United Kingdom', 
                'CA':'Canada', 'MX':'Mexico'}

Sequences are indexed by range of numbers but dictionaries are indexed by keys. A key can be of any immutable type although using a string or a number is most common. Tuples can be used as keys as long as the elements of the tuples are immutable.

Here are some of common operations on Dictionaries

Operations Example Output Comments
Get value given a key country_codes['US'] 'United States' Dictionary keys are unique. Value can be any complex type including ordered or unordered collection
Change value for a key
country_codes['US'] = 'USA'
'USA' Dictionaries are mutable so values can be changed
Delete a key
del country_codes['US']
{'UK': 'United Kingdom', 'CA': 'Canada', 'MX': 'Mexico'} A key can be deleted
Get a list of keys keys = list(country_codes.keys())
['UK','CA', 'MX'] You can check on the data type of keys by invoking print(type(keys)). You will notice that it is of type list
Sort the keys keys = sorted(country_codes.keys())
['CA', 'MX', 'UK'] A sorted list is returned
Construct a dict from sequence of key-value paris
dict([('UK', 'United Kingdom'),('MX','Mexico')])
{'MX': 'Mexico', 'UK': 'United Kingdom'} dict is one of the built in function
Construct a dict using arguments dict(UK = 'United Kingdom', MX ='Mexico') {'MX': 'Mexico', 'UK': 'United Kingdom'} Keys have to be strings for this to work

Points to note

  • If you try to access a key which is not present in the dictionary, KeyError is thrown


Set data structure is an unordered collection with no duplicate elements. Using curly braces { } you can create a set. There is also a set() function you can use to create a Set. Since empty curly brace { } is used to create both Set and Dictionary, to create an empty set you have to use set() function.

Here is an example:

americas = {'Canada', 'United States', 'Mexico', 'Mexico'}

Output: {'United States', 'Canada', 'Mexico'}

You will notice that duplicate 'Mexico' is not added. Also please note that your output may be different from the one shown. It may or may not be in the same order. Print output order is not predictable. Hence the term unordered collection. If you want the sequence order to be maintained then you should use one of the ordered collection types.

You can also construct using set() function. Here we create a set out of list elements.

americas = set(['Canada', 'United States', 'Mexico'])

Output: {'United States', 'Canada', 'Mexico'}

If you do not put the square braces and also remove comma between words, then a set is formed with all the letters in the words.

americas = set('Canada' 'United States' 'Mexico')

Output: {'c', 'e', ' ', 'i', 'd', 'n', 'M', 'o', 'S', 't', ',', 's', 'a', 'U', 'C', 'x'}

You will notice that the result is not only case sensitive but also duplicate letters are eliminated. Notice only one letter 'a' is present in the set and similarly other duplicate letters are eliminated.

Set operations

Let us consider another set of three countries which were English colonies:

old_eng_col = {'Canada', 'India', 'Australia', 'United States'}

Then, here are some of the common set operations you can perform on both the sets and their results:

Operation Example Result Comments
americas & old_eng_col
{'United States', 'Canada'} A new set is created with the elements which are common in both sets.
americas | old_eng_col
{'United States', 'India', 'Australia', 'Canada', 'Mexico'} A new set with all unique elements from both sets.
americas - old_eng_col
{'Mexico'} Elements in americas but not in old_eng_col

results matching ""

    No results matching ""