Data Structures in Python

Gupta Vaishali
4 min readMay 10, 2021

Data Structures are a particular way of organizing data in a computer. I am going to talk about different types of data structures namely, lists, tuples and dictionary in this article.

Lists

A list is a kind of collection which means in a single variable we can have more than one thing. For example, friends = [‘Joe’, ‘Glenn’, ‘Sam’]. By putting these in a square bracket, these names are assigned to a single variable called “friends” and it becomes a list. The names here are called list constants and the elements in the list are separated by commas. A list can also be empty. A list can be of either kind which means that it can contain a string, an integer or a floating-point number.

Another feature of lists is that, unlike strings, they are mutable. In other words, you can change the elements of a particular list, remove or add any element using the index operator.

We can create an empty list from scratch and then add elements using the append method. This way the list stays in order and new elements are added at the end of the list.

Now we will use data structures to create an empty list, add elements to that list using the append function and then calculate the average of those elements by using a while loop.

We create an empty list called num_list and then append various numbers to this list. After that, we calculate an average by dividing the sum of all the numbers in the list by the total count of number (calculated with len function).

Dictionaries

As we read about the lists above, they are a very ordered collection of values. A dictionary is more of a mess, elements are assigned and given a label or a key value. Dictionaries allow us to do fast databases like operations in Python. They are more like a bag of values in which each value is assigned a label. For example, a bag contains six books, four pens and three tissues. So the label here are books, pens and tissues with their values as six, four and three, respectively.

Let’s look at how to code to create a dictionary.

bag = dict()
bag["books"] = 6
bag["pens"] = 4
bag["tissues"] = 3
print(bag)
bag["books"] = 8
print(bag)
Output:{'books': 6, 'pens': 4, 'tissues': 3}
{'books': 8, 'pens': 4, 'tissues': 3}

Like lists, dictionaries are mutable too. They are very similar, the difference being, lists have positions and dictionaries have a label. Lists always start with zero but this is not true for dictionaries. To conclude, the structure of both is the same, however, the key mechanism of creating and changing them is different.

Tuples

Tuples are another kind of sequence that functions much like a list, they have elements that are indexed starting at 0. The main difference between a tuple and a list is that tuples are immutable, they cannot be changed. Once you create a tuple, you cannot alter its contents, similar to a string.

Since Python does not have to build tuple structures to be modifiable, they are simpler and more efficient in terms of memory use and performance than lists.

Hope this was helpful. Stay tuned for more insights on the python programming language.

--

--

Gupta Vaishali

I am a finance professional learning programming languages to become a fintech analyst.