Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.


Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons

LoginGet Started
← Back to Lessons
Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge
Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast
  • List

Edit on Github

Working with Lists in Python

What is a Python List?
Adding Elements to Python Lists (append and insert)

If you want to know how to create lists, access elements within lists, manipulate lists, and perform common operations such as sorting, filtering, and transforming, this article is for you. Whether you're a beginner or an experienced programmer, this article provides a comprehensive guide. The step-by-step instructions for Python lists and clear explanations make it easy to understand and apply the concepts.

What is a Python List?

A list is, normally, any collection of values. The rules of how to add or remove elements from that list can change from one programming language to another. But – generally – they are the only ways for developers to create elements. Lists are not the only way we have to store multiple values at once, but it is the most used tool for that purpose. For example: a list of students, a list of artists, a list of transactions... anything!

This primitive data-type does a lot more stuff than the others.

Every list has the same basic concepts:

  • The items: are the actual values inside in each position of the list.

  • The length: is the size of the list (how many items the list has).

  • Index: is the position of the element.

What is a list

☝ List positions start a zero (0); the first element is the element in the position zero (0)

How to Declare a Python List?

These are different examples of list declarations:

1my_empty_list = [] # Empty list 2my_list = ["Apple", "Orange", "Donkey"] # The only way to declare a "list" - a mutable and ordered collection of items 3my_tuple = ("Apple", "Orange", "Donkey") # This is a "tuple" - a more limited, ordered, but immutable collection of items 4my_set = {"Apple", "Orange", "Donkey"} # This is a "set" - a more limited, unordered and immutable collection of items

Access Items in the List

To access a specific element in a list, you need an index. We call index the integer value that represents the position of the element you want to access/get/retrieve.

The index must always start at zero (0). That means that a List of 2 items can have index=0 or index=1. Trying to get index=2 will return an IndexError because it will mean that we are trying to access the third element (which does not exist). For example, to get any items in a list you can do the following:

1print(my_list[0]) # Prints the 1st element on the console 2 3aux = my_list[3] 4print(aux) # Prints the 4th element on the console 5 6print(my_list[len(my_list) - 1]) # Prints the last element on the console 7print(my_list[-1]) # Also prints the last element on the console

Update Items in the List

If you want, you can reset or update any item inside a list using its index, like this:

1my_list[5] = 'Whatever value' 2# Assign a value to the 6th element on the list

Adding Elements to Python Lists (append and insert)

Using append in Python Lists

The first way is to add the element to the end of the list. You should use this method every time you can because it's a lot faster than insert.


Using insert in Python Lists

Using insert is easier for the developer because it will let you pick the positions in which you want to insert the element, but it is a slower method (worse performance):


Removing Elements from a Python List (pop, remove, delete)

Python has many ways to delete an element from a list.

Using pop

Without an argument, the pop() method will remove the last element. With an argument it will remove the element at that index.


Using remove

The remove method will let you remove the first occurrence of an element by its name.


Using delete

It will allow you to delete many items at once, you have to specify the starting position and ending position.


Looping Lists

Python for loop

Normally, when you manipulate lists, you have to loop all the items. For example: order them manually, flip them, filter them, etc. There are many ways you can loop an entire list, but the most used one is the for loop.


Looping using a position

Sometimes it is useful to loop the array using each element's position (index). We can do that by using the range() function. By default, the range will start from index zero and continue until a specified number is reached, not including that index number:


It is also possible to specify the starting index in the range, as well as the increment, by adding a starting point (the first parameter), and an increment value (the last parameter) in the range method:


Python .map() method

Similar to JavaScript Python has a .map() method, which will iterate over an array list and will call a lambda function for each element of the list.


The map method will automatically run the lambda function and will pass to it each element from the list happy_people as an argument. The code after the colon : in the lambda is the return of the function.

By default, the map() method in Python does not return a list-formatted output, like you may expect it to do from JavaScript. Instead, it returns a map object reference in memory, which looks something like this:

1# using the last code sample: 2print(result) # Output <map object at 0x0000002C59601748>

To make use of such a map object, most commonly you will need to convert it into an iterable by casting it into a list, using the list() method:


You can read more related articles at 4Geeks and keep on the Geek side!