Data Structures in Python 3

Python 3 data structures(list, tuple, dict, set)

The simplest way to talk about data structures is by thinking of the data structure as a way of organizing and storing data so it can be accessed and modified efficiently. How to store, insert, delete or access data efficiently led to the development of the data structures. One of the fundamental concepts of computer science are data structures along with algorithms.

General Data Structures

Most of the data structures could be divided into two categories:

Primitive data structures as: Integer, Float, String, Boolean.

Non-primitive data structures as: Arrays, Lists, Sets, Files.

In the next sections, we will make an overview of Non-primitive data structures in Python.

Linear Data Structures

The data structure where data items are organized sequentially or linearly is called a linear data structure. In the linear data structure, data items are organized linearly, one after another and only one data item can be reached. Example of linear data structures:

  • Array: series of a collection of elements(array elements) all of which are the same type, and has fixed size.
  • Linked List: data structure consisting of a collection of nodes that represent a sequence. Linked lists are useful for dynamic memory allocation.
  • Queue: data structure where one end is used to insert data (enqueue) and the other end is used to remove data (dequeue). Queue order is FIFO(First In First Out).
  • Stack: a data structure that follows a particular order for performing operations. The order is LIFO(Last In First Out) and it is a dynamic and constantly changing object.

Non-linear Data Structures

Opposite of linear data structure, is a non-linear data structure, data items are not arranged in a sequential structure and are connected to several other data items to represent a specific relationship. Example of non-linear data structures:

  • Tree: a data structure that may have multiple relations among its nodes that simulates a hierarchical tree structure.
  • Heap: sometimes called as the binary heap is a binary tree data structure that satisfies the heap property.
  • Hash Table: or hash map is a data structure that map keys to values and using a hash function it computes an index from which value can be found.
  • Graph: set of items connected by edges.

Python Data Structures

There are four major data structures used in Python: List, Tuples, Dictionaries, Sets.

List

A comma-separated list of items that don't need to be of the same type. Following is a simple example:

countries = ['Spain', 'Italy', 'Brazil', 'Japan']

Python lists are mutable, that means you can remove, change or add an item to the list by accessing it directly. Using the append() method you can add a new item to the list:

countries = ['Spain''Italy''Brazil''Japan']

countries.append('Malta') print(countries) 

# prints ['Spain', 'Italy', 'Brazil', 'Japan', 'Malta']

Using the remove() method you can remove an item from the list. Using the len() method you can check the number of items.

countries = ['Spain','Italy','Brazil','Japan']

len(countries) # returns 4

countries.remove('Spain')

len(countries) # returns 3

Tuples

As opposite of List, Tuples are immutable. That means you can't add or remove items from the tuple. You can find items in a Tuple, and you can use "in" operator to check if an item exists in the Tuple. Creating a tuple is very simple as putting different comma-separated values inside round brackets. Following is a simple example:

planets = ('Mars','Venus','Saturn')

len(planets) # Returns 3

Dictionaries

Python dictionaries are data structures that have key: value pairs. They are mutable, that means you can add, change or remove items from dictionaries. Following is a simple example:

person = {

    'name':'Sara',

    'age':25,

    'city': London

}

You can add multiple items to the dictionary with update() method.

people.update({'name': 'Kim', 'age': 18}, {'name': 'Alex', 'age': 32}) 

# two more objects are added to the people dict

Getting dictionary value is simple as providing key to the dict:

person = {'name': 'Pablo', 'ssn': 24214212}

print(person['ssn']) # 24214212

Set

In Python, the same as mathematics Set is an unordered data structure, in which you can have unique items. Following is a simple example:

random_numbers = set((1, 3, 5, 6, 6, 6, 9, 11))

print(random_numbers) # set([1, 3, 5, 6, 9, 11])

You can add or remove items from set:

engineers = set(['Jon', 'Maja', 'Spart'])

engineers.add('Marvin') # add new element

engineers.remove('Jon')

print(engineers) # set(['Maja', 'Spart', 'Marvin']

Conclusion

Use lists when you care about the order and you need a simple, iterable collection of data that does not need random access. 

Use a set when you don't need a duplicate, ordered elements. Also important to remember is that a set can not hold unhashable data types.

Use tuples when you defining constant values. Also when you will only need to iterate over it because tuples will be faster then lists.

Use dictionaries when you have unique keys that map to some values.