Data structures and comprehensions

( 8 users )

Python provides a range of inbuilt classes and objects that enables us to use various well known data structures (list, stack, queues, sets etc.) in our applications. Pythons also provides us with some comprehensions which helps us to concisely represent a part of code or data structure. Lets see some of these.

1. List

A list object is implicitly created as soon as we declare a list object.

countries = ["United States", "India", "Germany", "Australia"]
print(countries) 
# Output -> ['United States', 'India', 'Germany', 'Australia']

Call append method to add an item to the list.

countries.append("Singapore")
print(countries) 
# Output -> ['United States', 'India', 'Germany', 'Australia', 'Singapore']

Call sort method to sort the list items

countries.sort()
print(countries) 
# Output -> ['Australia', 'Germany', 'India', 'Singapore', 'United States']

To reverse sort the items in the list, just pass reverse=True in the function argument.

countries.sort(reverse=True) 
print(countries) 
# Output -> ['United States', 'Singapore', 'India', 'Germany', 'Australia']

Insert an item at second position

countries.insert(2, "Egypt")
print(countries) 
# Output -> ['United States', 'Singapore', 'Egypt', 'India', 'Germany', 'Australia']

Remove at item from end of the list

countries.pop()
print(countries) 
# Output -> ['United States', 'Singapore', 'Egypt', 'India', 'Germany']

2. Using list as stack (LIFO)


countries = ["United States", "India", "Germany", "Australia"]

countries.append("Singapore")
print(countries) # Output -> ['United States', 'India', 'Germany', 'Australia', 'Singapore']

countries.pop()
countries.pop()
print(countries) # Output -> ['United States', 'India', 'Germany']
		

3. Using list as queue (FIFO)

To implement a queue, we need to first import deque class from collections module. (Note : More on modules will be covered in later chapters)


from collections import deque
countries = deque(["United States", "India", "Germany", "Australia"])
countries.append("Singapore")
print(countries) # Output -> deque(['United States', 'India', 'Germany', 'Australia', 'Singapore'])

print("Pop -> ", countries.popleft()) # Pop ->  United States
print("Pop -> ", countries.popleft()) # Pop ->  India
print(countries) # Output -> deque(['Germany', 'Australia', 'Singapore'])
		

4. Comprehensions

Comprehension in Python means to represent code in more concise way. One of the feature of Python is that it is syntactically more concise and complex statements can be written simply in a line or two. Lets see this.

Suppose we have a list of numbers and we want to square each elements of that list. So one way to do this is :


nums = [1, 2, 3, 5, 8, 6]
squares = []

print("Numbers -> ", nums) # Output -> Numbers ->  [1, 2, 3, 5, 8, 6]
for i in nums:
  squares.append(i**2)
print("Square of numbers -> ", squares) 
# Output -> Square of numbers ->  [1, 4, 9, 25, 64, 36]
		

Another concise or comprehensive way of writing the same code is :


print("Numbers -> ", nums) # Output -> Numbers ->  [1, 2, 3, 5, 8, 6]
squares = [x**2 for x in nums]
print("Square of numbers -> ", squares) # Output -> Square of numbers ->  [1, 4, 9, 25, 64, 36]
		

See, how in single statement ([x**2 for x in nums]) we have computed the desired result.

More examples are:

Sum of every i-th element of two list in a new list


x = [1,2,3,4,5,6]
y = [9,8,7,6,5,4]
sum = [x[i]+y[i] for i in range(0,6)]
print(sum) # Output -> [10, 10, 10, 10, 10, 10]
		

Matrix Addition


x = [[1,2,3], [1,2,3], [1,2,3]]
y = [[3,2,1], [3,2,1], [3,2,1]]

matAdd = [[x[i][j]+y[i][j] for i in range(0, 3)] for j in range(0, 3)]
print(matAdd) # Output -> [[4, 4, 4], [4, 4, 4], [4, 4, 4]]
		

Flattening a two dimension list in one dimension


flattened_x = [x[i][j] for i in range(0, 3) for j in range(0, 3)]
print(flattened_x) # Output -> [1, 2, 3, 1, 2, 3, 1, 2, 3]
		

5. Deleting items using del

Consider the following set

x = [1,2,3,4,5,6]

Deleting first element from list


del x[0]
print(x) 
# Output -> [2, 3, 4, 5, 6]
		

Deleting items x[1], x[2], using [n:m] selects a range starting from index n to index (m-1) in the list


del x[1:3]
print(x) 
# Output -> [2, 5, 6]
		

Deleting all items, using [:] selects all items in the list


del x[:]
print(x) 
# Output -> []
		

Delete the list object


y = [9,8,7,6,5,4]
del y
print(y) 
# Output -> NameError: name 'y' is not defined
		

6. Tuples

A tuple is a collection of comma separated items. Its a way of packing different values of same or different datatypes.


a = 123, "Hello", 34.445, "Alice"
print(a) 
# Output -> (123, 'Hello', 34.445, 'Alice')
		

Tuples can be unpacked into variables


w, x, y, z = a
print(w, x, y, z) 
# Output -> 123 Hello 34.445 Alice
		

Tuples can be nested


a = 123, "Hello", 34.445, "Alice"
b = 345, "World", 77.434, "Bob"
c = a, b
print(c) 
# Output -> ((123, 'Hello', 34.445, 'Alice'), (345, 'World', 77.434, 'Bob'))
		

Nested tuples can be unpacked into other tuples like this


a, b = c
print(a, b) 
# Output -> (123, 'Hello', 34.445, 'Alice') (345, 'World', 77.434, 'Bob')
		

Nested tuples can be unpacked into other variables like this


(s,t,u,v), (w,x,y,z) = c
print(s,t,u,v, w, x, y, z) 
# Output -> 123 Hello 34.445 Alice 345 World 77.434 Bob
		

7. Sets

A set is defined as an unordered collection with no duplicates. It can be created by using set() function or with "{...}"


countries = {"United States", "India", "Germany", "Australia"}
		

To check if a value exists in a set or not, this is what we need to code.


print("India" in countries) 
# Output -> True
		

Set operations can be performed between two sets. Lets consider the below two sets.


countries1 = {"United States", "India", "Germany", "Australia"}
countries2 = {"Singapore", "India", "Canada", "Germany"}
		

The following set operations can be performed on the dataset.

  • Set A - Set B (Values in Set A that are not in set B)

    print(countries2 - countries1) 
    # Output -> {'Singapore', 'Canada'}
    
    print(countries1 - countries2) 
    # Output -> {'United States', 'Australia'}
  • Set A | Set B (Union of Set A and Set B)

    print (countries1 | countries2) 
    #Output -> {'United States', 'Singapore', 'Australia', 'India', 'Canada', 'Germany'}
  • Set A & Set B (Intersection of Set A and Set B)

    print(countries1 & countries2)
    # Output -> {'Germany', 'India'}
  • Set A ^ Set B ( Union(A,B) - Intersection(A,B) : Values in Set A and Set B but not in both)

    print(countries1 ^ countries2)
    # Output -> {'United States', 'Singapore', 'Australia', 'Canada'}

    This is equal to

    print((countries1 | countries2) - (countries1 & countries2))
    # Output -> {'Australia', 'United States', 'Canada', 'Singapore'}

    And also equals to (comprehensive way)

    a = { x for x in (countries1 | countries2) if x not in (countries1 & countries2)}
    print(a)
    # Output -> {'Australia', 'United States', 'Canada', 'Singapore'}

Python jupyter notebook for code examples

Access Jupyter notebook for this chapter here:
Python Notebook - Python Data Structures

As you can see, this was a long chapter but certainly important one because it covers certain aspects of Python programming language which you will user very frequently and can be seen in most of Python codes. So, congratulations on covering all these aspects both theoretically and practically. Lets see more of Python on upcoming chapters.

To Do

* Note : These actions will be locked once done and hence can not be reverted.

1. Track your progress [Earn 200 points]

2. Provide your ratings to this chapter [Earn 100 points]

0
Functions and lambda expressions
Modules and Packages
Note : At the end of this chapter, there is a ToDo section where you have to mark this chapter as completed to record your progress.