DEV Community

Cover image for Data Structures In Python - Part 2
Introschool
Introschool

Posted on

Data Structures In Python - Part 2

What is the Dictionary?
Dictionaries are an unordered collection of objects. It means that the Dictionary doesn’t support indexing, instead of it, Dictionary is a collection of key-value pairs. In the List, each value has an index but in the Dictionary each value is associated with the particular key, that’s why the Dictionary is an unordered collection of different values or object. To create a dictionary curly braces{ } are used.
Image description
How to Define a Dictionary?
To create a dictionary curly braces{ } are used. There are various ways you can define a dictionary. See the below code.

d = {'game': 'Cricket', 'type': 'One-day', 'over': 50, 'players': 11}
type(d)
# Output: <class 'dict'>

print(d)
# Output
# {'game': 'Cricket', 'type': 'One-day', 'over': 50, 'players': 11}

# dict() function
'''
You can pass the list of tuples, where each tuple will be a key-value pair.
'''
t = dict([('game', 'Cricket'),('type', 'One-day'),('over', 50),('players', 11)])
'''
You can also use mixed type keys to create a dictionary.
'''
c = dict([(1,[1, 2, 3]), ('name', 'Ramesh')])
Enter fullscreen mode Exit fullscreen mode

How to Access Dictionary Value?
You can access a dictionary value by its key. Just like List, you can use square brackets to access a value, Instead of an index, write a key in the brackets. See the below code.

# Accessing a dictionary value
d = {'game': 'Cricket', 'type': 'One-day', 'over': 50, 'players': 11}

print(d['game'])
# Output: Cricket

print(d['over'])
# Output: 50
Enter fullscreen mode Exit fullscreen mode

Dictionaries are Mutable
Yes, Dictionaries are mutable, you can change the dictionary value by using its key. See the below code.

# Changing Dictionary Values
c = dict([(1,[1, 2, 3]), ('name', 'Ramesh')])

print(c)

# Output
# {1: [1, 2, 3], 'name': 'Ramesh'}


'''
Suppose if you want to change the 'name' in this dictionary then how would you do that?
'''
c['name'] = 'Kumar' 

print(c)
# Output
# {1: [1, 2, 3], 'name': 'Kumar'}
Enter fullscreen mode Exit fullscreen mode

The ‘in’ Operator
You can use ‘in’ operator to check whether the key present in the dictionary or not. If the key is present, it will return True otherwise False.

# The 'in' Operator
d = {'game': 'Cricket', 'type': 'One-day', 'over': 50, 'players': 11}

print('game' in d)
# Output: True

print('type' not in d)
# Output: False
Enter fullscreen mode Exit fullscreen mode

Set
What is Set?
Set is an unordered collection of immutable data values. It means that the List and the Dictionary cannot be a value in the Set. The Set doesn’t contain duplicate values, all values in the set will be unique. Set is unordered collection because it doesn’t support indexing. To define a set you can use curly braces or Python’s built-in function set().
Image description
How to Define Set?
To define a set you can use either { } or you can use Python’s built-in function set(). The set() function takes any data object which has an index like String, List, and Tuples. See the below code. To define an empty set, instead of curly braces use set() function. The interpreter assumes { } as dictionary.

# Define an empty Set
b = {}
t = set()

type(b)
# Output: <class 'dict'>

type(t)
# Output: <class 'set'>

set1 = set('Books')
set2 = set([1,1,2,3,4,4,5,6])
set3 = set(('one', 'two', 'one', 'three', 'four', 'two'))

print('set1:', set1)
print('set2:', set2)
print('set3:', set3)

# Output
# set1: {'B', 'k', 's', 'o'}
# set2: {1, 2, 3, 4, 5, 6}
# set3: {'four', 'two', 'one', 'three'}

Enter fullscreen mode Exit fullscreen mode

How to Add or Remove Element from the set?
You can not access an individual value in the set but you can definitely add or remove the value. See the below code. To add the value in the set you can use built-in function add() and for removing the value you can use discard() function.

# Adding the value
s = {4, 'Text', True, False, 3.14}
s.add('Hello')

print(s)
# Output
# {False, True, 3.14, 4, 'Text', 'Hello'}

# Removing the value
s.discard(True)
print(s)

# Output
# {False, 3.14, 4, 'Text', 'Hello'}

Enter fullscreen mode Exit fullscreen mode

Set Operations
You can perform all the operations that you can perform on the mathematical set. Let’s see them in detail.

Union
The Union of two sets is a set of all values present in both the sets. You can use | operator or union() method.

# Union of sets

x = {1, 4, 6, 3, 9}
y = {'a', 'b', 'c', 4}

xy = x | y
print('xy', xy)
# Output
# {'c', 1, 3, 4, 6, 9, 'a', 'b'}

# Using union() function.
x.union(y)
# Output
# {'c', 1, 3, 4, 6, 9, 'a', 'b'}

y.union(x)
# Output
# {'c', 1, 3, 4, 6, 9, 'a', 'b'}
Enter fullscreen mode Exit fullscreen mode

Intersection
The intersection of two sets is the set of common values present in each set. You can use & operator or intersection() method.

# Intersection
x = {1, 4, 6, 3, 'c'}
y = {'a', 'b', 'c', 4}

# Using & operator
x_and_y = x & y
print(x_and_y)
# Output
# {'c', 4}


# Using intersection() method.
x.intersection(y)
# Output: {'c', 4}

y.intersection(x)
# Output: {'c', 4}
Enter fullscreen mode Exit fullscreen mode

Difference
The difference of two sets is the set of values that are only present in the one set not in the other set. See the below code.

# Difference
x = {1, 4, 6, 3, 'c'}
y = {'a', 'b', 'c', 4}

# Using ‘-’ operator
x_diff_y = x - y
print(x_diff_y)
# Output
#{1, 3, 6}


y_diff_x = y - x
print(y_diff_x)
# Output
# {'a', 'b'}

# Using difference() method.
x.difference(y)
# Output:{1, 3, 6}

y.difference(x)
# Output: {'a', 'b'}

Enter fullscreen mode Exit fullscreen mode

Symmetric Difference
The symmetric difference of two sets is the set of values that are present in both the sets except that are common values. You can use ^ operator or symmetric_difference() method to get the symmetric difference.

# Symmetric Difference
x = {1, 4, 6, 3, 'c'}
y = {'a', 'b', 'c', 4}

# Using '^' operator
x_sdiff_y = x ^ y
print(x_sdiff_y)

# Output
# {1, 3, 6, 'a', 'b'}


# Using symmetric_difference() method.
x.symmetric_difference(y)
# Output:{1, 3, 6, 'a', 'b'}

y.symmetric_difference(x)
# Output: {1, 3, 6, 'a', 'b'}

Enter fullscreen mode Exit fullscreen mode

The ‘in’ Operator
Now you are familiar with the ‘in’ operator. See the below code.

# The 'in' Operator
y = {'a', 'b', 'c', 4}

print('c' in y)
# Output: True

print(5 not in y)
# Output: True
Enter fullscreen mode Exit fullscreen mode

Discussion (1)