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
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.
Exercise 6-c: Calling List Elements (Negative Index)
And the last element.
Print the last element of the list through variable answer_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.
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.
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:
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.
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.
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.
Exercise 6-i: Removing the last item of a list (.remove() method)
Using .remove() method, clear the last element of the list.
Exercise 6-j: Reversing a Python list (.reverse() method)
Using .reverse() method, reverse the list.
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.
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?
Exercise 6-m: Max Function to Get the Lowest Value in a List
What is the minimum value in the list?
Exercise 6-n: Max Function to Get the Highest Value in a List
What is the maximum value in the list?