python data structure for beginners

Python Data Structures For Beginners

Last Updated | Dec, 29, 2020 By Kingsley Ijomah
Ever wondered how some programmers take one look at a problem and immediately know what code to write? maybe they take one look at your code and make a suggestion you never thought about? I will let you in on the secret. It is all about data structures. Let's explore this secret more closely.

The inner workings of any program have everything to do with the structure of the data you feed into it, once you understand this then what to do next becomes a matter of fact.

The data structure is simply the structure used to hold data together and so to manipulate any data ( programming ) you first need to know what structure that data is in and then use the structures methods accordingly.

There are four built-in data structures in Python - List, Tuple, Dictionary and Set, in this article you will see how to use each of them, and even more importantly when to use them.

At the end of this article, when you come across a problem you will immediately know what data structure best suits and which of its methods to execute and in what order.

Ready to explore data structures in python? let's do it...

Python Data Structures For Beginners

In this article, we are going to explore these four built-in data structures:

  1. List - ordered mutable
  2. Tuple - ordered immutable
  3. Dictionary - mutable key/value pair
  4. Set - mutable unordered unique cannot contain mutable types

Before you go into detail about each of these data structures, there are some definitions you should know to help you understand the data structures better.

Mutable / Immutable

Some data structures are mutable which means once created their content can be changed meaning you can add more items into the structure.

Immutable means the opposite, you cannot change this data structure once it is created. If you must change it, then you have to first make a copy and with the change, this is more expensive as you have to keep both copies.

Ordering / Sequence

So, what is a Python sequence ordering? A sequence is a group of items with a deterministic (predictable) ordering. The order in which we put them in is the order in which we get an item out from the structure.

Sequence data structures also have in and not in expressions (check if an item exists in the structure) it also has indexing operations (find items based on its index)

We will explore this in detail with each of the data structures.

Python Collections

Python collection is not deterministic (predictable) ordering, unlike sequences. They do not have a defined order you cannot refer to them by their index position, examples would be set and dictionary data structures.

python-datastructures-explained-for-beginners.png

1. LIST DATA STRUCTURE

list-data-structure-in-python Python List Data Structure

Defining a List
A list is a data structure that holds a collection of ordered/sequence mutable items.

Because of the nature of list as a mutable and ordered data structure, it is very popular and has a wider range of uses.

A list in python is created with square brackets containing comma-separated-values, you cannot miss it once you see one, below is an example of a list stored within a variable called fruits.

Example:
list = [1, 2, 3]

When to use List
Below are some reasons why you might need to use the list data structure instead.

  • You are dealing with sequenced data
  • Order might be of importance
  • Need to change the list (mutate)
  • The list is homogeneous ( same type of data )


For how to create and use list, with some examples checkout:
Python Tuple Data Structure Practical Example

2. TUPLE DATA STRUCTURE

tuple-data-structure Python Tuple Data Structure

Defining a Tuple

A tuple is an ordered immutable data structure, it is a list of values separated by a comma, just like a list, the difference is that a Tuple cannot be modified.

Meaning that the elements of a tuple cannot be modified once they are created, but if the element itself is a mutable data type like list, then that element can be modified.

Example:
tuple = (1, 2, 3)

When to use Tuple
Below are some reasons why you might need to use the tuple data structure instead.

  • You are dealing with sequenced data
  • Accessing index might be necessary
  • Cannot edit data once created
  • The list is heterogeneous ( different data types )


For how to create and use tuple, with some examples checkout:
Python Tuple Data Structure Practical Example

3. DICTIONARY DATA STRUCTURE

dictionary-data-structure Python Dictionary Data Structure

Defining a Dictionary

Python dictionary is an unordered collection of key/value pair, imagine this to be an address book where you need to know a person name (key) to retrieve their full information.

Python dictionaries are specified within curly brackets, each key/value pair is divided by a colon, and the pair is separated by a comma.

Example:
dict = {'firstname': 'John', 'lastname': 'Doe'}

When to use Dictionary
Below are some reasons why you might need to use the dictionary data structure instead.

  • Order is not important
  • Need to access an element by known unique keys
  • Want to be able to modify elements


For how to create and use a dictionary, with some examples checkout:
Python Dictionary Data Structure Practical Example

4. SET DATA STRUCTURE

set-data-structure Python Set Data Structure

Defining Set Data Structure

A set is an unordered collection with no duplicate elements, although mutable, and can have different types (integer, tuple, string etc). It cannot contain mutable elements such as lists, or dictionaries as its elements.

Set is created by putting all elements inside curly braces { } separated by a comma, it can also be created with the built-in set() function which accepts a single iterable argument.

Example:
set = {2.0, (1, 2, 3), 'Hello'}

When to use Set
Below are some reasons why you might need to use the set data structure instead.

  • Order is not important
  • Must not have duplicate elements
  • You can change elements after they are created
  • Will not contain mutable types e.g (list, sets, dictionary)
  • You need to perform a set union operation
  • You need to perform a set Intersection operation
  • You need to perform a set Difference operation
  • You need to perform a set Symmetric Difference operation


For how to create and use a dictionary, with some examples checkout:
Python Set Data Structure Practical Example

MY STORY

My name is Kingsley Ijomah, I am the founder of CODEHANCE, an online education platform built with you in mind, a place where I express my gratitude to a skill ( coding ) which has changed my life completely.

Learn To Code With Me.
FREE course included.

GET IN TOUCH

Enter your email address:

Delivered by FeedBurner