DATA STRUCTURES IN PYTHON


Data structures are used to organize the data in computer memory. The knowledge of data structure is required to write efficient computer programs. Depending upon the programming problem, in python we can choose from given data structures to efficiently manage the program data, to write efficient python scripts:

  • List
  • Dictionary
  • Tuple
  • Set

Python List

Python list is a collection of comma separated values enclosed within the square brackets. Python list is similar to the concept of array in C programming and java, however unlike array, python list can contain values of different data types.

num = [1,2,3,4,5] 
info = ["Ram",10,"kathmandu",2000.75] # can contain values of different data types

print(num)
print(info)

We can get length of list by using len() function- It returns number of elements in the list:

num = [1,2,3,4,5] 
print(f'Length of list is: {len(num)}')

Just like strings, python lists can also be sliced and indexed:

num = [10,20,30,40,50]
print(num[0]) # returns first element in the list
print(num[len(num)-1]) # returns last element in the list

# python list also support negative indexing
print(num[-1]) # returns last element
print(num[-2]) # returns second last element

Slicing operation in list is used to get sublist of the given list as follows:

# List of numbers
num = [10, 20, 30, 40, 50, 60, 70, 80]

# Slicing the list to get elements from index 0 up to (but not including) index 2
print(num[0:2])  # Output: [10, 20]

# Slicing the list to get elements from the beginning up to (but not including) index 2
print(num[:2])   # Output: [10, 20]

# Slicing the list to get elements from index 3 up to (but not including) index 6
print(num[3:6])  # Output: [40, 50, 60]

# Slicing the list to get elements from index 2 to the end of the list
print(num[2:])   # Output: [30, 40, 50, 60, 70, 80]

List also support concatenation operation:

# Lists of numbers
a = [10, 20, 30]
b = [50, 60]

# Concatenating lists 'a' and 'b' using the '+' operator
c = a + b

# Printing the result of the concatenation
print(c)  # Output: [10, 20, 30, 50, 60]

Let us check the data type of list variable:

a = [10,20,30]
print(type(a))

Its output is:

<class 'list'>

As you can see the data type for variable “a” is class<list>, it suggests “a” is an instance variable of class list, hence variable “a” can access all the methods defined for the list class. Most commonly used list methods are as follows:

  • append(x): Adds an element to the end of the list.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # Output: [1, 2, 3, 4]
  • extend(iterable): Extends the list by appending elements from an iterable.
my_list = [1, 2, 3]
my_list.extend([4, 5, 6])
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]
  • insert(i, x): Inserts an element at a specified position.
my_list = [1, 2, 3]
my_list.insert(1, 4)
print(my_list)  # Output: [1, 4, 2, 3]
  • remove(x): Removes the first occurrence of a specified element.
my_list = [1, 2, 3, 2, 4]
my_list.remove(2)
print(my_list)  # Output: [1, 3, 2, 4]
  • pop([i]): Removes and returns the element at the specified position, or the last element if no index is provided.
my_list = [1, 2, 3, 4]
popped_element = my_list.pop(2)
print(popped_element)  # Output: 3
print(my_list)         # Output: [1, 2, 4]
  • index(x[, start[, end]]): Returns the index of the first occurrence of a specified element.
my_list = [10, 20, 30, 20, 40]
index = my_list.index(20)
print(index)  # Output: 1
  • count(x): Returns the number of occurrences of a specified element.
my_list = [1, 2, 2, 3, 2, 4]
count = my_list.count(2)
print(count)  # Output: 3
  • sort(key=None, reverse=False): Sorts the elements of the list in ascending order.
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
my_list.sort()
print(my_list)  # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
  • reverse(): Reverses the order of the elements in the list.
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list)  # Output: [5, 4, 3, 2, 1]
  • copy(): Returns a shallow copy of the list.
my_list = [1, 2, 3]
copied_list = my_list.copy()
print(copied_list)  # Output: [1, 2, 3]

Tuple


In python, tuple data structure is collection of heterogeneous data elements enclosed within ‘()’ separated by comma.

t = (10,20,24.5,'hello')

print(t)

print(type(t))

Elements of tuples are accessed using index, the indexing of tuple is similar to indexing of string and list, as given below:

print(t[0])

print(t[:3])

print(t[-1])

As the above examples show, like a list, tuples are also collections of elements. The major difference between tuples and list is : Tuples are immutable collections of elements unlike List. Once tuples are created they cannot be updated or modified:

k = (10,20,30,40,50)

#Tuples are Immutable
# trying to update value at index 0, results in assignment error
k[0] = 100

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-5caddbe2829f> in <cell line: 1>()
----> 1 k[0] = 100

TypeError: 'tuple' object does not support item assignment
# unlike tuples, Lists are mutable and supports item assignments
l = [10,20,30]
l[0] = 100
print(l)

Output: 

[100, 20, 30]

Empty tuple can be created using tuple() method:

#empty tuple:
t = ()
print(len(t))

Tuple with single element can be created using single data element with trailing comma:

# tuple with single element
t = 'hello',
print(len(t))

In python, multiple comma separated values can be packed as a tuple as shown below:

k = 10,'20',2.45,'hello world'
print(k)

print(type(k))

# its reverse is also possible 
a,b,c,d = k
print(a)
print(b)
print(c)
print(d)

Output:

10
20
2.45
hello world

Built-in methods in Tuple

count(): Returns the number of occurrences of a specified value in the tuple.

my_tuple = (1, 2, 3, 2, 4, 2)
print(my_tuple.count(2))  # Output: 3

index(): Returns the index of the first occurrence of a specified value in the tuple.

my_tuple = (1, 2, 3, 2, 4, 2)
print(my_tuple.index(2))  # Output: 1

len(): Returns the number of elements in the tuple.

my_tuple = (1, 2, 3, 4, 5)
print(len(my_tuple))  # Output: 5

max(): Returns the maximum value in the tuple.

my_tuple = (10, 5, 20, 15)
print(max(my_tuple))  # Output: 20

min(): Returns the minimum value in the tuple.

my_tuple = (10, 5, 20, 15)
print(min(my_tuple))  # Output: 5

sorted(): Returns a new sorted list from the elements of the tuple.

my_tuple = (10, 5, 20, 15)
sorted_tuple = sorted(my_tuple)
print(sorted_tuple)  # Output: [5, 10, 15, 20]

Dictionary


Another useful, built-in data structure in python is Dictionary. Dictionary is key:value pair enclosed within the curly braces {}. Dictionaries are also called an associative array in other programming languages.

The key of a dictionary should alway be unique. The value of dictionaries are accessed using its unique key. Generally, dictionaries are used to store information related to any physical or logical entities.

# Let us store information related to a PRODUCT using dictionary
product = {'name':'Rice','price':'120','unit':'kilogram'}
print(product)

The value of dictionaries are accessed using its unique key:

print(product['name'])
print(product['price'])
print(product['unit'])

Output:

Rice
120
kilogram

We can create empty dictionary using {} or dict() method:

d = {}
# or 
# d = dict()
d['name']='Ram'
d['age'] = 12
d['address']='kathmandu,Nepal'
print(d)

Output:

{'name': 'Ram', 'age': 12, 'address': 'kathmandu,Nepal'}

The sequence of key,value pairs can also be used to create dictionary:

d = dict([(a,10),(b,20),(c,30)])
print(d)

Output:

{10: 10, '20': 20, 2.45: 30}

Most commonly used Built-in dictionary methods:

get(): Returns the value for a specified key. If the key is not found, it returns a default value (None by default).

my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.get('a'))  # Output: 1
print(my_dict.get('d'))  # Output: None
print(my_dict.get('d', 'Key not found'))  # Output: Key not found

keys(): Returns a view object that displays a list of all the keys in the dictionary.

my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.keys())  # Output: dict_keys(['a', 'b', 'c'])

values(): Returns a view object that displays a list of all the values in the dictionary.

my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.values())  # Output: dict_values([1, 2, 3])

items(): Returns a view object that displays a list of tuples containing the key-value pairs in the dictionary.

my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.items())  # Output: dict_items([('a', 1), ('b', 2), ('c', 3)])

pop(): Removes the item with the specified key and returns its value. Raises a KeyError if the key is not found.

my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.pop('b')
print(value)  # Output: 2
print(my_dict)  # Output: {'a': 1, 'c': 3}

popitem(): Removes and returns the last inserted (key, value) pair as a tuple. Raises a KeyError if the dictionary is empty.

my_dict = {'a': 1, 'b': 2, 'c': 3}
item = my_dict.popitem()
print(item)  # Output: ('c', 3)
print(my_dict)  # Output: {'a': 1, 'b': 2}

Sets


Sets in python are an unordered collection of unique elements. The comma separated values are enclosed within curly braces in sets.

# set is unordered collection of unique values
a = {'a','b','c','k',10,10,'a'}
print(a)

print(type(a))

Set can be created using built-in set() method:

k = set('aabbccddakkbb')
print(k)

Set in python  supports all the mathematical set operations like union,intersection etc:

# letters in a but not in b
a-b

# letters in a or b or both
a | b

# letters in both a and b
a & b

# letters in a or b but not both
a^b

Most commonly used built-in set methods:

add(): Adds a single element to the set.

my_set = {1, 2, 3}
my_set.add(4)
print(my_set)  # Output: {1, 2, 3, 4}

remove(): Removes a specified element from the set. Raises a KeyError if the element is not present.

my_set = {1, 2, 3}
my_set.remove(2)
print(my_set)  # Output: {1, 3}

discard(): Removes a specified element from the set if it is present, but does not raise an error if the element is not present.

my_set = {1, 2, 3}
my_set.discard(2)
print(my_set)  # Output: {1, 3}
my_set.discard(4)  # No error raised
print(my_set)  # Output: {1, 3}

pop(): Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.

my_set = {1, 2, 3}
element = my_set.pop()
print(element)  # Output: 1, 2, or 3 (random)
print(my_set)  # Output: {2, 3} or {1, 3} or {1, 2}

clear(): Removes all elements from the set.

my_set = {1, 2, 3}
my_set.clear()
print(my_set)  # Output: set()

union(): Returns a new set containing all the unique elements from both sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set)  # Output: {1, 2, 3, 4, 5}

intersection(): Returns a new set containing only the elements that are common to both sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set)  # Output: {3}