Python Data Structures

Data structure is a format of data organization in computer memory. Data structures are important part of a programming language. Python has four built-in data structures. They are:

  1. Lists,
  2. Tuples,
  3. Dictionaries, and
  4. Sets.

In this chapter, we will discuss all four of the above mentioned data structures. After finishing this chapter, you will be able to work with any of these data structure types.

Lists

Lists are one of the most commonly used data structures in python. It is a collection of items in a particular order. Each item in a list holds a relative position with respect to the other items.

How to create a list in python?
Creating a list in Python is incredibly easy. Simply define a name for your list, use an equal sign, and then list all the items within square brackets, separated by commas and enclosed in quotation marks. Imagine you love eating fruits and want to keep a record of all the fruits you have enjoyed so far. Let us create a list of these fruits as an example.


fruits = ['apple', 'banana', 'orange']

Our fruits list has three items. It is possible to create a list with less items and more items, as well. The highest number of items a list can include depends on the size of your memory, system architecture, etc. We can also create an empty list in python. How to create an empty list in python? Simply define a name, then use an equal sign followed by a pair of square brackets.


empty_list = []

How to access an individual element in a list?
After a few days you forgot what was the first fruit you have tested! Since you have saved them in a python list, you can retrieve the first item of the list to see the name of that fruit. Because python list is an ordered collection, you can access any element stored in it by mentioning the index (position number). Write the list name followed by 0 wraped in square brackets inside a print() function to view the first fruit name. Python indexing or positioning number starts from zero. The first element can be accessed by index 0, the second by index 1, and so on. Always remember that index number is 1 less than the actual position of the element.

print(fruits[0])

Suppose you told your friend about your fruits list and he wants to have a look at the list of fruits. You can do this with the help of for loop. You don't need to worry about the length of the list. Simply define a for loop as given below. The fruit variable in the code accesses one fruit name at a time and prints it. This process is repeated until all the fruit names are printed.


fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
    print(fruit)

How to make a list of numbers? Well, you can do that manually by typing in all the numbers into a list. But surely no one would want to do it. Time to recall the range() function of python. We know this function can create numbers, which we will use to our own benefit here.


numbers = list(range(10))
print(numbers)

We have wraped range(10) function with list() function. As a result the number 0 to 9 produced by the range(10) function is stored into the list named numbers. print(numbers) command show the numbers zero throgh nine.

Dictionaries

A dictionary in Python is a collection of key-value pairs. Each key is connected to a value. These keys can be used to access the values associated with them.

How to create a dictionary in Python? Creating a dictionary in Python is easy. Let us start by creating a simple dictionary that has exactly one key-value pair. Suppose you want to store information of your car in a dictionary e.g. what brand is your car!

my_car = {'brand': 'Toyota'}

The code above creates a dictionary named my_car which stores the color information of your car. In this dictionary, the string 'brand' is a key and its associated value is 'Toyota'.

How to add new key-value pairs to a dictionary? Now that you have created a dictionary, you can add new information or key-value pairs to it at any given time. Suppose you decide to add the information of color and build to your dictionary. You can easily add this information using the code given below:

my_car['color'] = 'White'
my_car['build'] = '2021'

The code above adds two new information to the my_car dictionay. Now it has three pieces of information in total.