Python List Exercises

In this page you can find interactive exercises to practice Python’s List data structure and its methods.

If you’d like to practice List Comprehensions, a more advanced Python List concept, you can click here.

Exercises aim to gradually increase in difficulty. 

So, let’s start.

Exercise 6-a: Calling Elements of a Python List (Index 0)

Assign the first element of the list to answer_1 on line 2


list[index]

Correct syntax to call a list’s element is brackets with the index number inside next to the list’s name: 

 

List index starts with zero.

answer_1 = lst[0]

Exercise 6-b: Calling List Elements and Indexing

And let's print the second element directly inside print function.


This time print the second element of the list directly on line 3. You should get 100.


Second element is index 1: [1]

print(lst[1])

Exercise 6-c: Calling List Elements (Negative Index)

And the last element.


Print the last element of the list through variable answer_1.


last element is easiest to print with reverse indexing, which starts with -1.

answer_1=lst[-1]

Exercise 6-d: Append method to add items to the end of Python Lists

.append method will let you add items to your lists.


On line 3, add the string "pajamas" to the list with .append() method.


you can add item(s) to lists by typing .append() after them with the item you’d like to add inside the parenthesis.

Don’t forget the quotes if you’re adding a string.

gift_list.append('pajamas')

Exercise 6-e: List inside List (Python Nested Data)

Lists can hold many type of data inside them. You can even add another list to a list as its element. This is called nested data in Python.


#On line 3, this time add the sub-list: ["socks", "tshirt", "pajamas"] to the end of the gift_list.


you can add item(s) to lists by typing .append() after them with the item you’d like to add inside the parenthesis.

Don’t forget the quotes if you’re adding a string.

gift_list.append(["socks","tshirt","pajamas"])

Exercise 6-f: Insert method to add to a specified index of a Python List

.insert() lets you specify the index you want to add your item.


On line 3, this time insert "slippers" to index 3 of gift_list.


.insert() takes 2 parameters:

1st: index number
2nd: element you’re adding:

list.insert(index, element)

Remember indexing in Python starts with 0.

So, first item in a list is index 0, second item in a list is index 1 and so on.

Also, you don’t have to make a new assignment since, .insert method changes a list in place. This also means you might want to be careful with it when you want to preserve your original list.

In those cases a good practice is creating a copy of the list and work on the copy. But, in this case you’re expected to make amends to the original.

gift_list.insert(2, "slippers")

Exercise 6-g: Index method to get the index of a List Item

With .index() method you can learn the index number of an item inside your list. Assign the index no of 8679 to the variable answer_1.


.index() takes 1 parameter which is the element you’re looking for. Just type it inside .index() and print it to see the value.

answer_1=lst.index(8679)

Exercise 6-h: Adding a different type of element to a List using Append

Using .append() method, add a new list to the end of the list which contains strings: "Navigator" and "Suburban".


.append() adds an item to the end of a list.

It’s very commonly used in programming and it can be very useful.

Lists are complex data types. This means it can also contain other sequences such as other lists, tuples, dictionaries etc.

This creates a nested data structure and it can be very useful for larger datasets with multiple categories, labels, subcategories, genres etc.

lst.append(["Navigator", "Suburban"])

Exercise 6-i: Removing the last item of a list (.remove() method)

Using .remove() method, clear the last element of the list.


Use the .remove() method. Inside parenthesis you can type the value you’d like to remove.

lst.remove(99)

Exercise 6-j: Reversing a Python list (.reverse() method)

Using .reverse() method, reverse the list.


.reverse() method reverses the index of a list.

lst.reverse()

Exercise 6-k: Count Method to get the amount of an item in a list

Using .count() method, count how many times 6 occur in the list.


.count() method tells how many times a value occur in a list. You need to type the value you’re checking inside .count()

answer_1=lst.count(6)

Exercise 6-l: Adding up all the items in a List w/ Sum Function

What is the sum of all the numbers in the list?


sum() function will calculate the total sum inside the numbers of your list.

answer_1 = sum(lst)

Exercise 6-m: Max Function to Get the Lowest Value in a List

What is the minimum value in the list?


min() function will show the minimum value of the list.

answer_1 = min(lst)

Exercise 6-n: Max Function to Get the Highest Value in a List

What is the maximum value in the list?


max() function will show the minimum value of the list.

answer_1 = max(lst)