“Python specific DS mastery”- Simplest to the core.

Karthika A
Guvi
Published in
17 min readSep 9, 2019

Containers

Data Structures are the programmatic way of storing data so that data can be used efficiently. Python has a few built-in data structures.

  • Set
  • List
  • Dictionary
  • Tuple

Set

A set is an unordered collection with no duplicate elements. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. Basic uses include membership testing.

Curly braces or the set() function can be used to create sets. Sets cannot have duplicated, there are usually used to build sequence of unique items.

Example:

basket = {‘apple’, ‘orange’, ‘apple’, ‘pear’, ‘orange’, ‘banana’}

print(basket) # show that duplicates have been removed

‘orange’ in basket #fast membership testing

‘cherry’ in basket

Screenshot:

Creating an empty set:

Empty curly braces {} will make an empty dictionary in Python. To make a set without any elements we use the set() function without any argument.

# initialize a with {}

a = {}

# check data type of a

print(type(a))

# initialize a with set()

a = set()

# check data type of a

print(type(a))

Screenshot:

Set Operations:

  • Set Union
  • Set Intersection
  • Set Difference
  • Set Symmetric Difference

Set Union:

Union of A and B is a set of all elements from both sets. Union is performed using ‘|’ operator. Same can be accomplished using the method union()

Union of A and B is a set of all elements from both sets.

Example:

# initialize A and B

A = {1, 2, 3, 4, 5}

B = {4, 5, 6, 7, 8}

print(A | B)

Screenshot:

Use union function

A.union(B)

B.union(A)

Screenshot:

Set Intersection:

Intersection is performed using & operator. Same can be accomplished using the method intersection().

Intersection of A and B is a set of elements that are common in both sets

# Initialize A and B

A = {1, 2, 3, 4, 5}

B = {4, 5, 6, 7, 8}

# use & operator

print(A & B)

# use intersection function

A.intersection(B)

B.intersection(A)

Screenshot:

Set Difference:

Difference of A and B (A — B) is a set of elements that are only in A but not in B. Similarly, B — A is a set of element in B but not in A

Difference is performed using — operator. Same can be accomplished using the method difference().

Example:

# Initialize A and B

A = {1, 2, 3, 4, 5}

B = {4, 5, 6, 7, 8}

# Use — operator on A

print(A — B)

#use difference function

A.difference(B)

B.difference(A)

Screenshot:

Set Symmetric Difference:

Symmetric Difference of A and B is a set of elements in both A and B except those that are common in both.

Symmetric difference is performed using ^ operator. Same can be accomplished using the method symmetric_difference().

Example:

# initialize A and B

A = {1, 2, 3, 4, 5}

B = {4, 5, 6, 7, 8}

# use ^ operator

print(A ^ B)

# use symmetric_difference function

A.symmetric_difference(B)

B.symmetric_difference(A)

Screenshot:

Different Python Set Methods:

Method

Description

add()

Add an element to a set

clear()

Remove all elements form a set

copy()

Return a shallow copy of a set

difference()

Return the difference of two or more sets as a new set

difference_update()

Remove all elements of another set from this set

discard()

Remove an element from set if it is a member. (Do nothing if the element is not in set)

intersection()

Return the intersection of two sets as a new set

intersection_update()

Update the set with the intersection of itself and another

isdisjoint()

Return True if two sets have a null intersection

issubset()

Return True if another set contains this set

issuperset()

Return True if this set contains another set

pop()

Remove and return an arbitrary set element. Raise KeyError if the set is empty

remove()

Remove an element from a set. If the element is not a member, raise a KeyError

symmetric_difference()

Return the symmetric difference of two sets as a new set

symmetric_difference_update()

Update a set with the symmetric difference of itself and another

union()

Return the union of sets in a new set

update()

Update a set with the union of itself and others

Built-in Functions with Set:

Function Description

all()

Return True if all elements of the set are true (or if the set is empty).

any()

Return True if any element of the set is true. If the set is empty, return False.

enumerate()

Return an enumerate object. It contains the index and value of all the items of set as a pair.

len()

Return the length (the number of items) in the set.

max()

Return the largest item in the set.

min()

Return the smallest item in the set.

sorted()

Return a new sorted list from elements in the set(does not sort the set itself).

sum()

Retrun the sum of all elements in the set.

Frozen set:

Frozenset is a new class that has the characteristics of a set, but its elements cannot be changed once assigned. While tuples are immutable lists, frozensets are immutable sets.

Sets being mutable are unhashable, so they can’t be used as dictionary keys. On the other hand, frozensets are hashable and can be used as keys to a dictionary.

Frozensets can be created using the function frozenset(). This datatype supports methods like copy(), difference(), intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() and union(). Being immutable it does not have method that add or remove elements.

Example:

# Initialize A and B

A = frozenset([1, 2, 3, 4])

B = frozenset([3, 4, 5, 6])

Screenshot:

LIST

Python offers a range of compound data types often referred to as sequences. List is one of the most frequently used and very versatile data type used in Python.

In Python programming, a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas. It can have any number of items and they may be of different types (integer, float, string etc.).

Example:

# Empty list

my_list = []

# List of integers

my_list = [1, 2, 3]

# List with mixed data types

my_list = [1, “Hello”, 3.4]

A list can even have another list as an item. This is called nested list.

# nested list

my_list = [“mouse”, [8, 4, 6], [‘a’]]

List Index:

We can use the index operator [] to access an item in a list. Index starts from 0. So, a list having 5 elements will have index from 0 to 4.

Trying to access an element other that this will raise an IndexError. The index must be an integer. We can’t use float or other types, this will result into TypeError.

Example:

my_list = [‘p’,’r’,’o’,’b’,’e’]

print(my_list[0])

print(my_list[4])

# Nested List

n_list = [“Happy”, [2,0,1,5]]

# Nested indexing

print(n_list[0][1])

print(n_list[1][3])

Screenshot:

Negative indexing:

Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.

Example:

my_list = [‘p’,’r’,’o’,’b’,’e’]

print(my_list[-1])

print(my_list[-5])

Screenshot:

Slicing:

We can access a range of items in a list by using the slicing operator (colon).

Example:

my_list = [‘p’,’r’,’o’,’g’,’r’,’a’,’m’,’i’,’z’]

# elements 3rd to 5th

print(my_list[2:5])

# elements beginning to 4th

print(my_list[:-5])

# elements 6th to end

print(my_list[5:])

# elements beginning to end

print(my_list[:])

Screenshot:

Slicing can be best visualized by considering the index to be between the elements as shown below. So if we want to access a range, we need two index that will slice that portion from the list.

Changing or adding elements to a list:

List are mutable, that the elements can be changed unlike string or tuple. We can use assignment operator (=) to change an item or a range of items.

Example:

# mistake values

odd = [2, 4, 6, 8]

### change the 1st item

odd[0] = 1

print(odd)

### change 2nd to 4th items

odd[1:4] = [3, 5, 7]

print(odd)

Screenshot:

append() methods:

Lists have several methods amongst which the append() and extend() methods. The former appends an object to the end of the list (e.g., another list) while the later appends each element of the iterable object (e.g., another list) to the end of the list.

Example:

stack = [‘a’,’b’]

stack.append(‘c’)

stack

#append several objects contained in a list,

stack.append([‘d’, ‘e’, ‘f’,’g’])

stack

Screenshot:

extend() methods:

The object [‘d’, ‘e’, ‘f’,’g’] has been appended to the exiistng list. However, it happens that sometimes what we want is to append the elements one by one of a given list rather the list itself. You can do that manually of course, but a better solution is to use the extend() method as follows:

Example:

stack = [‘a’, ‘b’, ‘c’]

stack.extend([‘d’, ‘e’,’f’])

stac

Screenshot:

Index ()

The index() methods searches for an element in a list.

Example:

my_list = [‘a’,’b’,’c’,’b’, ‘a’]

my_list.index(‘b’)

It returns the index of the first and only occurence of ‘b’. If you want to specify a range of valid index, you can indicate the start and stop indices.

Example:

my_list = [‘a’,’b’,’c’,’b’, ‘a’]

my_list.index(‘b’, 2)

if the element is not found, an error is raised

Screenshot:

Insert()

You can insert element wherever you want in a list. Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

Example:

my_list = [‘a’,’b’,’c’,’b’, ‘a’]

my_list.insert(2, ‘a’)

my_list

Screenshot:

sort()

There is a sort() method that performs an in-place sorting.

Example:

my_list = [‘a’,’b’,’c’,’b’, ‘a’]

my_list.sort()

my_list

Screenshot:

There is also the possibility to sort in the reverse order:

Example:

my_list.sort(reverse=True)

my_list

Screenshot:

Reverse()

You can reverse the element in-place.

Example:

my_list = [‘a’, ‘c’ ,’b’]

my_list.reverse()

my_list

Screenshot:

Remove()

You can remove the first occurrence of an element

Example:

my_list = [‘a’,’b’,’c’,’b’, ‘a’]

my_list.remove(‘a’)

my_list

Screenshot:

Delete or remove elements from a list:

We can delete one or more items from a list using the keyword del. It can even delete the list entirely.

Example:

my_list = [‘p’,’r’,’o’,’b’,’l’,’e’,’m’]

# delete one item

del my_list[2]

print(my_list)

# delete multiple items

del my_list[1:5]

print(my_list)

# delete entire list

del my_list

# Error: List not defined

Print(my_list)

Screenshot:

Python List Methods

append() — Add an element to the end of the list

extend() — Add all elements of a list to the another list

insert() — Insert an item at the defined index

remove() — Removes an item from the list

pop() — Removes and returns an element at the given index

clear() — Removes all items from the list

index() — Returns the index of the first matched item

count() — Returns the count of number of items passed as an argument

sort() — Sort items in a list in ascending order

reverse() — Reverse the order of items in the list

copy() — Returns a shallow copy of the list

List Comprehension:

List comprehension is an elegant and concise way to create new list from an existing list in Python. List comprehension consists of an expression followed by for statement inside square brackets.

Example:

pow2 = [2 ** x for x in range(10)]

pow2

Screenshot:

A list comprehension can optionally contain more for or if statements. An optional if statement can filter out items for the new list.

Example:

pow2 = [2 ** x for x in range(10) if x > 5]

odd = [x for x in range(20) if x % 2 == 1]

Screenshot:

DICTIONARY

A dictionary is a sequence of items. Each item is a pair made of a key and a value. Dictionaries are not sorted. Dictionaries are unordered set of key: value pairs where keys are unique. We declare dictionaries using {} braces. We use dictionaries to store data for any particular key and then retrieve them.

Pairs of keys and values are specified in a dictionary by using the notation d = {key1 : value1, key2 : value2 }. Notice that the key-value pairs are separated by a colon and the pairs are separated themselves by commas and all this is enclosed in a pair of curly braces.

The dictionary allows you to associate one piece of data (a “key”) with another (a “value”). Dictionaries don’t support the sequence operation of the sequence data types like strings, tuples and lists. Dictionaries belong to the built-in mapping type.

Create a dictionary:

Creating a dictionary is as simple as placing items inside curly braces {} separated by comma. An item has a key and the corresponding value expressed as a pair, key: value. While values can be of any data type and can repeat, keys must be of immutable type (string, number or tuple with immutable elements) and must be unique.

Example

# empty dictionary

my_dict = {}

# dictionary with integer keys

my_dict = {1: ‘apple’, 2: ‘ball’}

# dictionary with mixed keys

my_dict = {‘name’: ‘John’, 1: [2, 4, 3]}

# using dict()

my_dict = dict({1:’apple’, 2:’ball’})

# from sequence having each item as a pair

my_dict = dict([(1,’apple’), (2,’ball’)])

Screenshot:

Access elements from a dictionary:

While indexing is used with other container types to access values, dictionary uses keys. Key can be used either inside square brackets or with the get() method. The difference while using get() is that it returns None instead of KeyError, if the key is not found.

Example:

my_dict = {‘name’:’Jack’, ‘age’: 26}

print(my_dict[‘name’])

print(my_dict.get(‘age’))

Screenshot:

Change or add elements in a dictionary:

We can add new items or change the value of existing items using assignment operator. If the key is already present, value gets updated, else a new key: value pair is added to the dictionary.

Example:

my_dict = {‘name’:’Jack’, ‘age’: 26}

# update value

my_dict[‘age’] = 27

print(my_dict)

# add item

my_dict[‘address’] = ‘Downtown’

print(my_dict)

Screenshot:

Delete or remove elements from a dictionary:

We can remove a particular item in a dictionary by using the method pop(). This method removes as item with the provided key and returns the value. The method, popitem() can be used to remove and return an arbitrary item (key, value) form the dictionary.

All the items can be removed at once using the clear() method. We can also use the del keyword to remove individual items or the entire dictionary itself.

Example:

#create a dictionary

squares = {1:1, 2:4, 3:9, 4:16, 5:25}

# remove a particular item

print(squares.pop(4))

print(squares)

# remove an arbitrary item

print(squares.popitem())

print(squares)

# delete a particular itemdel squares[5]

del squares[5]

print(squares)

# remove all items

squares.clear()

print(squares)

# delete the dictionary itself

del squares

Screenshot:

Python Dictionary Methods

Method

Description

clear()

Remove all items form the dictionary.

copy()

Return a shallow copy of the dictionary.

fromkeys(seq[, v])

Return a new dictionary with keys from seq and value equal to v(defaults to None).

get(key[,d])

Return the value of key. If key does not exit, return d (defaults to None).

items()

Return a new view of the dictionary’s items (key, value).

keys()

Return a new view of the dictionary’s keys.

pop(key[,d])

Remove the item with key and return its value or d if key is not found. If d is not provided and key is not found, raises KeyError.

popitem()

Remove and return an arbitrary item (key, value). Raises KeyError if the dictionary is empty.

setdefault(key[,d])

If key is in the dictionary, return its value. If not, insert key with a value of d and return d (defaults to None).

update([other])

Update the dictionary with the key/value pairs from other, overwriting existing keys.

values()

Return a new view of the dictionary’s values

Dictionary Comprehension:

Dictionary comprehension is an elegant and concise way to create new dictionary from an iterable in Python. Dictionary comprehension consists of an expression pair (key: value) followed by for statement inside curly braces {}.

Example:

squares = {x: x*x for x in range(6)}

print(squares)

This code is equivalent to

squares = {}

for x in range(6):

squares[x] = x*x

Screenshot:

A dictionary comprehension can optionally contain more for or if statements. An optional if statement can filter out items to form the new dictionary.

Example:

odd_squares = {x: x*x for x in range(11) if x%2 == 1}

Screenshot:

TUPLE:

Tuples are used to hold together multiple objects. Similar to lists. One major feature of tuples is that they are immutable like strings i.e. you cannot modify tuples. Tuples are defined by specifying items separated by commas within an optional pair of parentheses.. the tuple of values used will not change.

Advantages of Tuple over List

  • We generally use tuple for heterogeneous (different) datatypes and list for homogeneous (similar) datatypes.
  • Since tuple are immutable, iterating through tuple is faster than with list. So there is a slight performance boost.
  • Tuples that contain immutable elements can be used as key for a dictionary. With list, this is not possible.
  • If you have data that doesn’t change, implementing it as tuple will guarantee that it remains write-protected.

Creating a Tuple:

A tuple is created by placing all the items (elements) inside a parentheses (), separated by comma. A tuple can have any number of items and they may be of different types (integer, float, list, string etc.).

Example:

# empty tuple

my_tuple = ()

print(my_tuple)

# tuple having integers

my_tuple = (1, 2, 3)

print(my_tuple)

# tuple with mixed datatypes

my_tuple = (1, “Hello”, 3.4)

print(my_tuple)

# nested tuple

my_tuple = (“mouse”, [8, 4, 6], (1, 2, 3))

print(my_tuple)

# tuple can be created without parentheses

my_tuple = 3, 4.6, “dog”

print(my_tuple)

# tuple unpacking is also possible

a, b, c = my_tuple

print(a)

print(b)

print(c)

Example 2:

zoo = (‘python’, ‘elephant’, ‘penguin’)

print(‘Number of animals in the zoo is’, len(zoo))

new_zoo = ‘monkey’, ‘camel’, zoo

print(‘Number of cages in the new zoo is’, len(new_zoo))

print(‘All animals in new zoo are’, new_zoo)

print(‘Animals brought from old zoo are’, new_zoo[2])

print(‘Last animal brought from old zoo is’, new_zoo[2][2])

print(‘Number of animals in the new zoo is’,len(new_zoo)-1+len(new_zoo[2]))

Screenshot:

The variable zoo refers to a tuple of items. That the len function can be used to get the length of the tuple. Shifting these animals to a new zoo since the old zoo is being closed.

Therefore, the new_zoo tuple contains some animals which are already there along with the animals brought over from the old zoo. Back to reality, note that a tuple within a tuple does not lose its identity.

We can access the items in the tuple by specifying the item’s position within a pair of square brackets just like we did for lists. This is called the indexing operator.

We access the third item in new_zoo by specifying new_zoo[2] and we access the third item within the third item in the new_zoo tuple by specifying new_zoo[2][2].

Accessing Elements:

1. Indexing

We can use the index operator [] to access an item in a tuple where the index starts from 0. So, a tuple having 6 elements will have index from 0 to 5.

Trying to access an element other that (6, 7…) will raise an IndexError. The index must be an integer, so we cannot use float or other types. This will result into TypeError.

Example:

my_tuple = (‘p’,’e’,’r’,’m’,’i’,’t’)

print(my_tuple[0])

print(my_tuple[5])

n_tuple = (“mouse”, [8, 4, 6], (1, 2, 3))

# nested index

print(n_tuple[0][3])

# nested index

print(n_tuple[1][1])

Screenshot:

2. Negative Indexing:

Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.

Example:

my_tuple = (‘p’,’e’,’r’,’m’,’i’,’t’)

print(my_tuple[-1])

print(my_tuple[-6])

Screenshot:

3. Slicing

We can access a range of items in a tuple by using the slicing operator — colon “:”.

Example:

my_tuple = (‘p’,’r’,’o’,’g’,’r’,’a’,’m’,’i’,’z’)

# elements 2nd to 4th

print(my_tuple[1:4])

# elements beginning to 2nd

print(my_tuple[:-7])

# elements 8th to end

print(my_tuple[7:])

# elements beginning to end

print(my_tuple[:])

Screenshot:

Slicing can be best visualized by considering the index to be between the elements. So if we want to access a range, we need the index that will slice the portion from the tuple.

Changing a Tuple:

This means that elements of a tuple cannot be changed once it has been assigned. But, if the element is itself a mutable data type like list, its nested items can be changed. We can also assign a tuple to different values (reassignment).

Example:

my_tuple = (4, 2, 3, [6, 5])

# we cannot change an element TypeError: ‘tuple’ object does not support item assignment. but item of mutable element can be changed

my_tuple[3][0] = 9

print(my_tuple)

# tuples can be reassigned

my_tuple = (‘p’,’r’,’o’,’g’,’r’,’a’,’m’,’i’,’z’)

print(my_tuple)

Screenshot:

Deleting a Tuple:

We cannot delete or remove items from a tuple. But deleting a tuple entirely is possible using the keyword del.

Example:

my_tuple = (‘p’,’r’,’o’,’g’,’r’,’a’,’m’,’i’,’z’)

del my_tuple

my_tuple

Screenshot:

Built-in Functions with Tuple:

Built-in functions like all(), any(), enumerate(), len(), max(), min(), sorted(), tuple() etc. are commonly used with tuple to perform different tasks.

Function

Description

all()

Return True if all elements of the tuple are true (or if the tuple is empty).

any()

Return True if any element of the tuple is true. If the tuple is empty, return False.

enumerate()

Return an enumerate object. It contains the index and value of all the items of tuple as pairs.

len()

Return the length (the number of items) in the tuple.

max()

Return the largest item in the tuple.

min()

Return the smallest item in the tuple

sorted()

Take elements in the tuple and return a new sorted list (does not sort the tuple itself).

sum()

Retrun the sum of all elements in the tuple.

tuple()

Convert an iterable (list, string, set, dictionary) to a tuple.

Heads up..Hats off for reaching this point..

Now,start using these data structure for real-time problem solving.

Be a proud GUVIan!!!

--

--