A Newbie’s Guide to Python Lists: My top 10 most useful list methods

Darren Willenberg
MLthinkbox
Published in
5 min readOct 24, 2022

An Introduction to Lists in Python

Lists are one of the most versatile data types in Python. They can be used to store different types of data and are often used to store lists of things, such as words or numbers.

Lists are used to store multiple items in a single variable. There are many different ways to create lists in Python, but the most common way is when you use the list() function with brackets or continuously append new values into the list from another source.

Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage. Python lists can be identified by square brackets “[]”.

Getting Started with List Operations

Below are my top 10 most useful list methods that every python beginner should know.

1. Append

My absolute most used method when programming is the append() method. It is used to insert a new item at the end of your list. This method takes the new value as an argument that will be inserted at the end of the list. This method is very often seen in combination with python loops.

list = [] # create empty list
list.append("Dog")
print(list)
OUTPUT: ["Dog"]

2. Count

The count() method is used to count the number of times that any given item appears in a list. This method takes the item value as an argument that will be searched for in the list and returns the number of appearances of the item in the list as a numerical value. If the item value does not exist in the list, then it will return with the value 0.

list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]
list.count("Dog")
OUTPUT: 2

3. Sum

The sum() method is used to calculate the total sum of all numbers in a list. This method takes the list as an argument and returns the sum of values as a result. Variables within the list must be of type integer of float for example.

numbers = [1,2,3,4,5,1,4,5]

Sum = sum(numbers)
print(Sum)
OUTPUT: 25

4. Sort

The sort() method is used to sort list data. This method is useful when you are working with the same type of list data and you need to organize the data ascending or descending for a specific purpose. This method does not take any argument and it by default returns the sorted list data in ascending order.

# Sorting text
list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]
list.sort()
print(list)
OUTPUT: ['Cat', 'Dog', 'Dog', 'Lion', 'Zebra']
# Sorting descending
list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]
list.sort(reverse=True)
print(list)
OUTPUT: ['Zebra', 'Lion', 'Dog', 'Dog', 'Cat']

5. Len

The len() method calculates the number of items contained within a list. This method takes the list as an argument and returns the length result.

list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]
len(list)
OUTPUT: 5

6. Index

The index() method is used to obtain the position of any item in a list. This method takes the search item value as the input and returns the positional value of the item in the list starting from zero. It generates a ValueError if no value exists. The location of duplicate values can be found by looping through the list while changing the starting location of the search.

# Returns location of search value
list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]
list.index("Zebra")
OUTPUT: 3
# Returns first location in list if duplicate values
list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]
list.index("Dog")
OUTPUT: 0

You can wrap your list analysis with “Try” and “Except” to programmatically handle ValueErrors.

list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]try:
list.index("Kangaroo")
except:
print("Value not found")
OUTPUT: Value not found

7. Insert

The insert() method is used to insert a new item into a particular position in the list. The insert() method contains two arguments. The first argument takes the position value where the new item will be inserted. The second argument takes the new item value.

list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]
list.insert(3, "Kangaroo")
print(list)
OUTPU: ['Dog', 'Cat', 'Dog', 'Kangaroo', 'Zebra', 'Lion']

8. Remove

The remove() method is used to remove a particular item from the list. This method takes the item value as an argument that will be removed from the list, if it exists. Remove targets from the first occurrence of a value found in the list. If the item value does not exist in the list, then a ValueError will be generated.

# Remove targets the first value found in the list
list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]
list.remove("Dog")
print(list)
OUTPUT: ['Cat', 'Dog', 'Zebra', 'Lion']

9. Extend

The extend() method is used to merge two list items and store the merged items in the first list. This method takes the second list as the argument and adds the values of the second list at the end of the first list.

# Two lists
first_list = ["Dog", "Cat", "Zebra", "Lion"]
second_list = ["Kangaroo", "Giraffe"]
# First list gets extended
first_list.extend(second_list)
print(first_list)
OUTPUT: ['Dog', 'Cat', 'Zebra', 'Lion', 'Kangaroo', 'Giraffe']

10. Copy

The copy() method is used to make a copy of a list. This method is useful for keeping original list values before modifying the list. This method does not take any argument as input, it just creates a duplicate copy of the list.

# original_list variable updates based on changes in list
list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]
original_list = list
print(original_list)
OUTPUT: ['Dog', 'Cat', 'Dog', 'Zebra', 'Lion']
list.remove("Dog")
print(original_list)
OUTPUT: ['Cat', 'Dog', 'Zebra', 'Lion']
# orignal_list is unchanged by previous operation if we use copy.
list = ["Dog", "Cat", "Dog", "Zebra", "Lion"]
original_list = list.copy()list.remove("Dog")
print(original_list)
OUTPUT: ['Dog', 'Cat', 'Dog', 'Zebra', 'Lion']

OUTRO

And that's my list of python list methods that every Pythonista should know. While I did not dive into overly complex applications of each method, I think that it is sufficient from a conceptual standpoint. Finally:

  • List are incredibly important to understand in python,
  • Having good control over list methods will enhance your abilities with other data structures such as tuples, arrays and even DataFrames later!

Thanks for reading!

References

--

--

Darren Willenberg
MLthinkbox

Engineer | Analyst | Data Science Enthusiast | UCT | MLthinkbox Publication Founder