An Introduction to lists, dictionaries and tuples in python for beginners

There are plenty of Python for beginners’ courses available on the internet for free where one can develop a strong understanding of the entire language however in this short python for beginners tutorial we’re going to understand what are data structures, how do we create them and why do we use them.

First let us ask the question, what are data structures?

Data structures can basically be thought of as containers which can hold multiple variables. Using a data structure is a lot more efficient than creating a new variable for each value. A single data structure in python can hold different data types, like int and string, together, so we don’t need to worry about declaring the data type of a data structure. We can use an iterator, like a for loop, to access all the values stored within it. We use data structures to store a collection of variables which are in some way or another related to each other. Storing our data in such a table format makes it easier to analyze our data.

In python, we have 3 major data structures that are most commonly used, which are Lists, Tuple, Dictionaries. We will take our time and go through each one of them with a great amount of detail

1)  Lists

This is a structure which can hold an ordered collection of variables. What do we mean by order? Well, that means that each value that is added to the list is in a sequence. The indexing of a list starts at 0.

Lists are used widely while creating applications, where we need to keep track of certain values. Once you create a list, you can add values to it as well as delete values from it. This feature is known as mutability and we say that lists in Python are mutable. We use square brackets to create a list and a comma to separate each value. String values are written inside a colon or semicolon whereas numbers are written just as they are.

The naming convention that one should follow during creating a list, is the same as the one we follow while naming a variable.

Example:

My_list = [‘Red’, ‘Blue’, ‘Green’]
Print (My_list[0])

OUTPUT:

Red

The following code creates a list named My_list and assigns 3 values to it. While the second line prints the first value.

2)  Dictionaries

Dictionaries in programming are very similar to dictionaries in language, in the sense that, every word in a dictionary has a meaning attached to it and every keyword in a python dictionary has a value attached to it. Also, whenever we want to look up the meaning, we search for the word. Similarly, when we need to access the value in a python dictionary, we call the keyword.

Why do we do this?

We do this when we want to relate two values with each other and using a keyword would be a much easier way to access the value. For example, suppose we wanted to store the addresses of people. We would create a dictionary with the name as a keyword and the address as a value. So when we want to access anyone’s address all we have to do is put in the name to find it.

How do we do this?

A dictionary in python is a key-value pair. Every key in a dictionary is unique since if we have the same key, identifying a value would not be possible. Another rule we must follow while naming our keys is that we can only use strings which do not include complex characters or numbers. The value on the other hand can take any value, string or number.

Unlike lists where we use square brackets to store a value, in a dictionary, we use curly brackets and each key-value pair is separated with a comma. Also, the key-value pairs are not ordered in any manner which means that it has no indexing. The syntax that we follow is – Dict_name = {key: value, key: value, key: value, …}

Example: 

Dict ={‘height’ : 6.9, ‘weight’: 70  , ’waist’: 35 }
Print(“My height is ”, Dict[‘height’] )
Dict[‘Legs’] = 2

OUTPUT:

My height is 6.9

In the above code, we create a dictionary name Dict and we assign three key-value pairs to it. We then access the value in our print statement. In the third line of code, we’re adding a new key ‘legs’ with a value of 2 to our original dictionary.

3)  Tuple

Just like the other two, tuples are also used to hold a collection of values. Tuples are extremely similar to lists. The only difference between the two is that tuples are immutable. What this means is that once we declare a tuple and add values into it we can’t add or delete values like we can with a list. Tuples are also an ordered sets of values which means they have an index.

We use a tuple when we know for sure that the value is not going to change, since it isn’t dynamic in nature. To separate a value in tuples we use a comma and the values are enclosed in parenthesis. The brackets are optional but it is always a good practice in coding to explicitly define a tuple rather than implying it implicitly.

CODE:

Colors = (‘red’, ‘blue’, ‘green’)  #create a tuple
Print(“My favourite colour is ”, Color[2])

OUTPUT:

My favourite colour is green

After reading this python for beginners’ tutorial, you should have a fair understanding of data structures in python. There are a few more data structures that you may come across as you keep learning more and more and work on more projects like series, sets, sequence etc. But remember the most important thing is to not get overwhelmed with everything and to keep practising every day like you would if you were learning how to play the guitar and one day, suddenly,  it will all make complete sense to you.