Published: Aug. 5, 2019 by lukemakin |  estimated reading time: 7 minutes
In a nutshell data structures are the way of storing and retrieving data, so we can easily perform various operations that will help us writing our code. Different data structures are designed for different circumstances and in this one we will take a closer look at the most common ones: lists, dictionaries, tuples, sets and frozen sets.

This part will cover lists. Let's get started!

company_cars = ['Volkswagen Golf', 'Mercedes CLS', 'Skoda Octavia', 'Renault Clio', 'Volkswagen Golf', 'Audi A4']

Lists are containers that can handle mutiple data types (strings, integers, objects etc.). They are also mutable (we can modify them), ordered (we can access the data by index position) and can carry duplicates (i.e. above you can notice 'Volkswagen Golf' appears twice). Let's begin to play with them!.

Checking the length of the list

len(company_cars)
# 6

Looping through the list:


for car in company_cars:
print(car)

'''
Volkswagen Golf
Mercedes CLS
Skoda Octavia
Renault Clio
Volkswagen Golf
Audi A4
'''

Looping through the list with the index of an element using enumerate():


for i,car in enumerate(company_cars):
print(i, car)

'''
0 Volkswagen Golf
1 Mercedes CLS
2 Skoda Octavia
3 Renault Clio
4 Volkswagen Golf
5 Audi A4
'''

Adding elements to the list at certain position, but before we change the size of our list, let's check the id of the our list stored in the memory:


id(company_cars)

# 4483737224

Now let's add another car to the collection:


company_cars[2] = 'BMW 3'

'''
Specifying the index where new element should be placed - in this case in third position (because indexing starts with 0):
['Volkswagen Golf',
'Mercedes CLS',
'BMW 3', <-------------
'Skoda Octavia',
'Renault Clio',
'Volkswagen Golf',
'Audi A4']

Can be done also with the insert() method:
'''

company_cars.insert(2, 'BMW 3')

And let's check the id again: 


id(company_cars)

# 4483737224

The id didn't change. That means we can modify the list without creating a copy. We'll see what the difference while talking about i.e. tuples (which are immutable). Let's continue and add an item to the list, but this time to it's end:


company_cars.append('BMW 3')

Extending a list with another list:


company_cars2 = ['Ford Mondeo', 'Fiat 500']
company_cars.extend(company_cars2)

'''
['Volkswagen Golf',
'Mercedes CLS',
'BMW 3',
'Skoda Octavia',
'Renault Clio',
'Volkswagen Golf',
'Audi A4',
'Ford Mondeo',
'Fiat 500',
'BMW 3',
'Ford Mondeo',
'Fiat 500']
'''

Counting particular elements in the list:


company_cars.count('Volkswagen Golf')
# 2

Removing elements from the list:


company_cars.remove('BMW 3')

'''
Removes the first element found:
['Volkswagen Golf',
'Mercedes CLS',
<----- this position was removed
'Skoda Octavia',
'Renault Clio',
'Volkswagen Golf',
'Audi A4',
'BMW 3', <------ this position isn't effected
'Ford Mondeo',
'Fiat 500']

Remove the last element of the list using pop():


company_cars.pop()
'''
['Volkswagen Golf',
'Mercedes CLS',
'Skoda Octavia',
'Renault Clio',
'Volkswagen Golf',
'Audi A4',
'BMW 3',
'Ford Mondeo']
'''

Remove particular element by specifying the index:


company_cars.pop(1)

'''
Mercedes CLS has been removed:
['Volkswagen Golf',
'Renault Clio',
'Volkswagen Golf',
'Audi A4',
'BMW 3',
'Ford Mondeo']
'''

Remove all the specified elements from the list using list comprehension:


company_cars = [car for car in company_cars if car != 'Volkswagen Golf']

'''
Both Volkswagen Golf's will be removed
['Renault Clio',
'Audi A4',
'BMW 3',
'Ford Mondeo']
'''

Sorting the cars by name:


company_cars.sort()

'''
['Audi A4',
'BMW 3',
'Ford Mondeo',
'Renault Clio']
'''

Merging two lists with each other using zip():


owners = ['Adam Smith', 'John Doe', 'Lisa Bell', 'Kate Underwoood']

cars_and_owners = zip(owners, company_cars)

for data in cars_and_owners:
print(data[0], data[1])

'''
Adam Smith Audi A4
John Doe BMW 3
Lisa Bell Ford Mondeo
Kate Underwoood Renault Clio
'''

And finally making a copy of the list, and clearing the original one:


copied_list = company_cars.copy()

company_cars.clear()

And that is it! We covered the most important methods. The best way to understand how they work is to try it on your own and simply experiment as much as you need: 


Extras
To view additional content login or create a free account
Categories:
Share your thoughts

No comments yet...

Signup to the newsletter
To get the latest updates from pyplane
© copyright 2019 pyplane.com