Python Lists

Home » Python » Python Lists

Table of Contents

Python lists
Python lists

Introduction to Python lists

Python lists are important to store data, we can store unrelated data however we want. It can be stored in alphabetical order or the some other order which depends on the data it contains.

We have listed the uses of Functions in Python list along with examples.

We have listed uses of available functions in Python list along with their examples.

Next, we will see how can we manipulate lists and extract data from them, sort them and much more.

What is a list?

A list is a collection of items in any particular order, you can make any thing into a list. You can put anything into the list which do not have to be related in any particular way. 

Since the list usually contains more than one item, it’s a good idea to make its name plural.

Example: Bicycles, Landmarks, Sites, etc.

Landmarks.py


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']

Accessing elements of a list

We can access any element of a list by its location which is also knows as its index.


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']

#This syntax is used to access a single element from a list using its index number.
print(Landmarks[0])

>>Colosseum

Please note that the index starts from ‘0’ not ‘1’, therefore the first element will have an index 0.

Using elements of a list

We can use the elements of a list like we use a variable.


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
message = “I went to the " + Landmarks[0].title() + “."

>>I went to the Colosseum.

Changing, Adding and Removing elements from Python lists

Changing elements in a list

We can change/modify an element from a list by the following method.


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
print(Landmarks)
Landmarks[0] = 'Machu Picchu'
print(Landmarks)

>>['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
>>['Machu Picchu','Gateway of India','Eiffel Tower','Angkor Wat']

Adding elements to a list

Add elements to the end of the list

We can add an element into the list by using the append() method.


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
Landmarks.append('Statue of Liberty')
print(Landmarks)

>>['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat','Statue of Liberty']

Adding elements by index

We can also add elements by its location(index) and insert() method.


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
Landmarks.insert(1,'Marine Drive')
print(Landmarks)

>>['Colosseum','Marine Drive','Gateway of India','Eiffel Tower','Angkor Wat']

Removing elements from a list

Removing using the del statement

We can remove any element from its location by using the del statement.


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
print(Landmarks)
del Landmarks[0]
print(Landmarks)

>>['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
>>['Gateway of India','Eiffel Tower','Angkor Wat']

Removing an item using the pop() method in the last position

The pop() method is similar to the del statement but it can store the deleted value to a variable and delete it at the same time.


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
print(Landmarks)
popped_landmark = Landmarks.pop()
print('We have removed ' + '"' + popped_landmark + '"' + ' from the Landmarks list.')
print(Landmarks)

>>['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
>>We have removed “Angkor Wat" from the Landmarks list.
>>['Colosseum','Gateway of India','Eiffel Tower']

Removing an item using pop() method from any position

We can also provide an index to the pop() method to remove(delete) any element we want.


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
print(Landmarks)
popped_landmark = Landmarks.pop(1)
print('We have removed ' + '"' + popped_landmark + '"' + ' from the Landmarks list.')
print(Landmarks)

>>['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
>>We have removed “Gateway of India" from the Landmarks list.
>>['Colosseum','Eiffel Tower','Angkor Wat']

Removing an item by its value

We can remove an item by its value by using the remove() method.


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
print(Landmarks)
Landmarks.remove('Eiffel Tower')
print(Landmarks)

>>['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
>>['Colosseum','Gateway of India','Angkor Wat']

Organising the list

Sorting a list with sort() method

The sort() method changes the list permanently.


Landmarks = ['Colosseum','Gateway of India','Eiffel Tower','Angkor Wat']
Landmarks.sort()
print(Landmarks)

>>['Angkor Wat','Collosseum','Eiffel Tower','Gateway of India']

Using sort method with parameter reverse=True, it will sort the list in reverse


Landmarks = ['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']
Landmarks.sort(reverse=True)

>>['Gateway of India','Eiffel Tower','Collosseum','Angkor Wat']

Sorting a list with sorted() function

We can also sort the list temporarily by using the sorted() function.


Landmarks = ['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']
print(Landmarks)
print(sorted(Landmarks))
print(Landmarks)

>>['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']
>>['Angkor Wat','Collosseum','Eiffel Tower','Gateway of India']
>>['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']

Reversing a list using reverse() method

We can reverse the entire list by using the reverse() method.


Landmarks = ['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']
print(Landmarks)
Landmarks.reverse()
print(Landmarks)

>>['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']
>>['Angkor Wat','Eiffel Tower','Gateway of India','Collosseum']

Finding a length of a list

We can find the length of a list with len() function.


Landmarks = ['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']
print(len(Landmarks))

>>4

Avoiding index errors while using a list

Let’s say you have a list of 4 items and you ask for fifth item, it will show you an error since index starts from 0.


Landmarks = ['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']
print(Landmarks[4])

>>Traceback (most recent call last): File “Landmarks.py", line 2, in print(Landmarks[4]) IndexError: list index out of range

Looping through a list

Print all the elements of a list one by one using a for loop.


Landmarks = ['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']
for landmark in Landmarks:
    print(landmark)

>>Collosseum
>>Gateway of India
>>Eiffel Tower
>>Angkor Wat

Using the elements as a variable and printing a message using a for loop.


Landmarks = ['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']
for landmark in Landmarks:
    print(“Hey!, I want to visit '" + landmark.title() + “' one day.")

>>Hey!, I want to visit 'Collosseum' one day.
>>Hey!, I want to visit 'Gateway Of India' one day.
>>Hey!, I want to visit 'Eiffel Tower' one day.
>>Hey!, I want to visit 'Angkor Wat' one day.

More example.


Landmarks = ['Collosseum','Gateway of India','Eiffel Tower','Angkor Wat']
for landmark in Landmarks:
    print(“Hey!, I want to visit '" + landmark.title() + “' one day.")
    print(“Do you want to come along with me?\n")

>>Hey!, I want to visit 'Collosseum' one day.
>>Do you want to come along with me?

>>Hey!, I want to visit 'Gateway Of India' one day.
>>Do you want to come along with me?

>>Hey!, I want to visit 'Eiffel Tower' one day.
>>Do you want to come along with me?

>>Hey!, I want to visit 'Angkor Wat' one day.
>>Do you want to come along with me?

Making a numerical list

We can make a list of any length by using the range() function and the list() function at the same time.


for value in range(1,10):
    print(value)

>>1
>>2
>>3
>>4
>>5
>>6
>>7
>>8
>>9

Note that the it prints from 1 to 9, not 10. Now, we are making a list of 100 elements which starts from 1 to 100.


numbers100 = list(range(1,101))
print(numbers100)

>>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

We can modify it to print only even numbers.


for n in range(1,13):
    print(n)

>>2
>>4
>>6
>>8
>>10
>>12

Min, Max and Sum of a list

We can get the minumum value, maximum value and sum of all the individual elements of a list by min() function, max() function and sum() function respectively.


mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

print(“The lowest number of mylist is: “) 
print(min(mylist))

print(“The highest number of mylist is: “)
print(max(mylist))

print(“The sum of mylist is: “)
print(sum(mylist)

>>The lowest number of mylist is:
>>1
>>The highest number of mylist is:
>>20
>>The sum of mylist is:
>>210


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *