1 of 29

Sets & Dictionaries

Python

2 of 29

Sets

  • Indentified by curly braces
    • {'Alice', 'Bob', 'Carol'}
    • {'Dean'} is a singleton
  • Can only contain unique elements
    • Duplicates are eliminated
  • Immutable like tuples and strings

3 of 29

Sets do not contain duplicates

  • >>> cset = {11, 11, 22}
  • >>> cset�{11, 22}

4 of 29

Sets are immutable

  • >>> aset = {11, 22, 33}
  • >>> bset = aset
  • >>> aset = aset | {55}
  • >>> aset�{33, 11, 22, 55}
  • >>> bset�{33, 11, 22}

Union of two sets

5 of 29

Sets have no order

  • >>> {1, 2, 3, 4, 5, 6, 7}�{1, 2, 3, 4, 5, 6, 7}
  • >>> {11, 22, 33}�{33, 11, 22}

6 of 29

Sets do not support indexing

  • >>> myset = {'Apples', 'Bananas', 'Oranges'}
  • >>> myset�{'Bananas', 'Oranges', 'Apples'}
  • >>> myset[0]�Traceback (most recent call last):� File "<pyshell#2>", line 1, in <module>� myset[0]�TypeError: 'set' object does not support indexing

7 of 29

Examples

  • >>> alist = [11, 22, 33, 22, 44]
  • >>> aset = set(alist)
  • >>> aset�{33, 11, 44, 22}
  • >>> aset = aset + {55}�SyntaxError: invalid syntax

8 of 29

Boolean operations on sets (I)

  • Union of two sets

  • Contains all elements that are in set A or in set B

A

B

9 of 29

Boolean operations on sets (II)

  • Intersection of two sets

  • Contains all elements that are in both sets A and B

A

B

10 of 29

Boolean operations on sets (III)

  • Difference of two sets

  • Contains all elements that are in A but not in B

A

B

11 of 29

Boolean operations on sets (IV)

  • Symmetric difference of two sets

  • Contains all elements that are either
    • in set A but not in set B or
    • in set B but not in set A

A

B

12 of 29

Boolean operations on sets (V)

  • >>> aset = {11, 22, 33}
  • >>> bset = {12, 23, 33}
  • Union of two sets
    • >>> aset | bset �{33, 22, 23, 11, 12}
  • Intersection of two sets:
    • >>> aset & bset�{33}

13 of 29

Boolean operations on sets (VI)

  • >>> aset = {11, 22, 33}
  • >>> bset = {12, 23, 33}
  • Difference:
    • >>> aset – bset�{11, 22}
  • Symmetric difference:
    • >>> aset ^ bset�{11, 12, 22, 23}

14 of 29

https://blog.hubspot.com/website/python-set

15 of 29

https://intellipaat.com/blog/tutorial/python-tutorial/data-structures-with-python-cheat-sheet/

16 of 29

17 of 29

18 of 29

Towards Dictionaries

  • Lists and tuples hold elements with only integer indices

  • So in essence, each element has an index (or a key) which can only be an integer, and a value which can be of any type (e.g., in the above list/tuple, the first element has key 0 and value 45)
    • What if we want to store elements with non-integer indices (or keys)?

45

“Coding”

4.5

7

89

0

1

2

3

4

Integer �Indices

19 of 29

Dictionaries

  • In Python, you can use a dictionary to store elements with keys of any types (not necessarily only integers like lists and tuples) and values of any types as well

  • The above dictionary can be defined in Python as follows:

dic = {"NUM":45, 1000:"coding", 2000:4.5, 3.4:7, "XXX":89}

45

“Coding”

4.5

7

89

“NUM”

1000

2000

3.4

“XXX”

keys of different types

Each element is a key:value pair, and elements are separated by commas

key

value

Values of different types

20 of 29

Dictionaries

  • In summary, dictionaries:
    • Can contain any and different types of elements (i.e., keys and values)
    • Can contain only unique keys but duplicate values

    • Can be indexed but only through keys (i.e., dic2[“a”] will return 1 but dic2[0] will return an error since there is no element with key 0 in dic2 above)

Output: {'a': 2, 'b': 2}

dic2 = {"a":1, "a":2, "b":2}

print(dic2)

The element “a”:2 will override the element “a”:1

because only ONE element can have key “a”

21 of 29

Dictionaries

  • In summary, dictionaries:
    • CANNOT be concatenated
    • CANNOT be repeated
    • Can be nested (e.g., d = {"first":{1:1}, "second":{2:"a"}}
    • Can be passed to a function and will result in a pass-by-reference and not pass-by-value behavior since it is immutable (like lists)

Output:

{'first': {1: 1}, 'second': {2: 'a'}}

{'first': [1, 2, 3], 'second': {2: 'a'}}

def func1(d):

d["first"] = [1, 2, 3]

dic = {"first":{1:1}, "second":{2:"a"}}

print(dic)

func1(dic)

print(dic)

22 of 29

Dictionaries

  • In summary, dictionaries:
    • Can be iterated over

Output:

dic = {"first": 1, "second": 2, "third": 3}

for i in dic:

print(i)

How to get the values?

first

second

third

ONLY the keys will be returned.

23 of 29

Dictionaries

  • In summary, dictionaries:
    • Can be iterated over

Output:

dic = {"first": 1, "second": 2, "third": 3}

for i in dic:

print(dic[i])

1

2

3

Values can be

accessed via indexing!

24 of 29

Adding Elements to a Dictionary

  • How to add elements to a dictionary?
    • By indexing the dictionary via a key and assigning a corresponding value

Output:

dic = {"first": 1, "second": 2, "third": 3}

print(dic)

dic["fourth"] = 4

print(dic)

{'first': 1, 'second': 2, 'third': 3}

{'first': 1, 'second': 2, 'third': 3, 'fourth': 4}

25 of 29

Adding Elements to a Dictionary

  • How to add elements to a dictionary?
    • By indexing the dictionary via a key and assigning a corresponding value

Output:

dic = {"first": 1, "second": 2, "third": 3}

print(dic)

dic[”second"] = 4

print(dic)

{'first': 1, 'second': 2, 'third': 3}

{'first': 1, 'second’: 4, 'third': 3}

If the key already exists, �the value will be overridden

26 of 29

Deleting Elements to a Dictionary

  • How to delete elements in a dictionary?
    • By using del

Output:

dic = {"first": 1, "second": 2, "third": 3}

print(dic)

dic["fourth"] = 4

print(dic)

del dic["first"]

print(dic)

{'first': 1, 'second': 2, 'third': 3}

{'first': 1, 'second': 2, 'third': 3, 'fourth': 4}

{'second': 2, 'third': 3, 'fourth': 4}

27 of 29

Deleting Elements to a Dictionary

  • How to delete elements in a dictionary?
    • Or by using the function pop(key)

Output:

dic = {"first": 1, "second": 2, "third": 3}

print(dic)

dic["fourth"] = 4

print(dic)

dic.pop(“first”)

print(dic)

{'first': 1, 'second': 2, 'third': 3}

{'first': 1, 'second': 2, 'third': 3, 'fourth': 4}

{'second': 2, 'third': 3, 'fourth': 4}

28 of 29

Dictionary Functions

  • Many other functions can also be used with dictionaries

Function

Description

dic.clear()

Removes all the elements from dictionary dic

dic.copy()

Returns a copy of dictionary dic

dic.items()

Returns a list containing a tuple for each key-value pair in dictionary dic

dic.get(k)

Returns the value of the specified key k from dictionary dic

dic.keys()

Returns a list containing all the keys of dictionary dic

dic.pop(k)

Removes the element with the specified key k from dictionary dic

29 of 29

Dictionary Functions

  • Many other functions can also be used with dictionaries

Function

Description

dic.popitem()

Removes the last inserted key-value pair in dictionary dic

dic.values()

Returns a list of all the values in dictionary dic