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...
In this article, we are going to explore these four built-in data structures:
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.
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.
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 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.
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.
For how to create and use list, with some examples checkout:
Python Tuple Data Structure Practical Example
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.
For how to create and use tuple
, with some examples checkout:
Python Tuple Data Structure Practical Example
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.
For how to create and use a dictionary
, with some examples checkout:
Python Dictionary Data Structure Practical Example
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.
For how to create and use a dictionary
, with some examples checkout:
Python Set Data Structure Practical Example