Data structures in Python

Introduction

Python is said to have batteries included. What this means is that you have a number of data-structure already built-in so that you can use them without importing any module.

It is said that you already have some library or functions for 80% of the use cases and there is 20% chance that you will write your own code.

This article is a gentle introduction to 4 data-structures in Python which is available without any import.

Here we go.

Lists, Tuples, Sets and Dictionaries


I am again stressing that you don't need to remember any of the stuffs mentioned here. There are ample of references and documentations available online which can be referred anytime.

List

Let us try to write a few points about lists in Python.

  1. A list is an array-like container.
  2. It can hold any mixed type of object, variable or function.
  3. It can grow or shrink in size at runtime.
  4. It can be iterated using a for loop.
  5. A item can also be accessed using its index using 0-based indexing system
  6. A list can be identified by looking at square brackets [] in a code.

[1, 'two', 3, 'four', 'five', 6, 7, 'eight']
[1, 'two', 3, 'four', 'five', 6, 7]
[1, 3, 'four', 'five', 6, 7]
1
3
four
five
6
7
[1, 3, 'four', 'five', 6, 7, 'a', 'e', 'i', 'o', 'u']
1
3
      

Tuple

  1. A tuple can be considered as a immutable list. It is a list which when created once can not be changed. It guarantees that any attempt to add, remove or update a tuple item is rejected.
  2. It can hold data of any type.
  3. It can be iterated using a for loop.
  4. Its element can also be accessed using 0-based indexing system.
  5. A tuple can be identified by looking at parenthesis () in a code.
()
(1,)
(1, 2, 'three', 4)
1
2
three
4
1
      

Set

A set as you know is a mathematical concept. A set in mathematics is defined as a collection of distinct objects. Simply put, it is a group of unique entities.

  1. A set is a grouping of unique objects.
  2. It is an un-ordered collection.
  3. It can store any type of data.
  4. Sets support addition of objects.
  5. It also supports union, interesection and difference of two sets.
  6. It can be iterated using a for loop.
  7. The order of items while iterating is not guaranteed to be same as order of items while inserting.

set()
{1}
{1, 2, 3, 4, 5, 'seven', 'six'}
{1, 2, 3, 4, 5, 'seven', 22, 'six'}
{1, 2, 3, 4, 5, 'seven', 22, 'six'}
{'a', 1, 'e', 22}
{1, 2, 3, 4, 5, 'seven', 'e', 22, 'a', 'six'} {1, 2, 3, 4, 5, 'seven', 'e', 22, 'a', 'six'}
{1, 22} {1, 22}
{2, 3, 4, 'seven', 5, 'six'} {2, 3, 4, 'seven', 5, 'six'}
a
1
e
22
      

Dictionary

A dictionary is collection of key-value pairs. In other languages, they are also known as maps and hash-maps. A python dictionary appears and behaves very similar to a JSON. A dictionary can map any hashable key (integers, strings, tuples, etc.) to any type of value.

A dictionary is most used data structure after lists. A clear understanding of it very necessary for any programmer.

  1. A dictionary is collection of key and value pairs separated by a colon(:).
  2. A dictionary in code may be mistaken as a set. Remember that a dictionary has a : after a key while set does not have a :.
  3. There can be one and only one key in a dictionary.
  4. The order of items while iterating is not guaranteed to be same as order of items while inserting.

{}
{'two': 2, '4': [4, 'four', 'chaar'], 3: 'three', 'one': 1}
dict_keys(['two', '4', 3, 'one'])
dict_values([2, [4, 'four', 'chaar'], 'three', 1])
dict_items([('two', 2), ('4', [4, 'four', 'chaar']), (3, 'three'), ('one', 1)])
{'two': 2, '4': [4, 'four', 'chaar'], 'hundred': 100, 3: 'three', 'one': 1}
{'two': 2, '4': [4, 'four', 'chaar'], 'hundred': 101010, 3: 'three', 'one': 1}
two 2
4 [4, 'four', 'chaar']
hundred 101010
3 three
one 1
      

Data Structures Revisited

List, Tuple, Set and Dicts (Dictionary) are very powerful containers and can do a lot more than what has been mentioned here. We will be covering each of them separately in different articles.

Below is very small code snippet which can be referred in case you find difficulty in identifying these containers.


 [1, 2, 3, 4, 5, 'six', set(), {1: 2, 3: 4}]
 (1, 2, 3, 'four')
 {1, 2, 3, 'four'}
 {'one': 1, 2: 'two', '3': [3, 'three', 'teen']}
      

Conclusions

We introduced ourselves about 4 basic containers available in Python. We talked about lists which are dynamic-arrays or vectors. We also talked about tuples which are immutable lists. We saw sets which are similar to their mathematical definitions and touched a bit upon the mighty dictionary which is a JSON-like containers which maps a key to a value. All of these data structures can hold values of all types.

Author image
New Delhi
I am a passionate programmer based in New Delhi, India. I mostly work with Python. Apart from work, I find myself doing poetry and learning about various languages. I prefer chai over coffee.