Tutorials  /  Python

How To add Elements to a List in Python

Ccentron Redaktion · December 2024 ·3 min read ·Python, Tutorial

In this tutorial, we will learn different ways to add elements to a list in Python.

VM

Try it yourself: cloud servers from €3.12/month

ccloud³ VMs from German data centers – deployed in seconds, billed by the hour, with a €200 starting credit for new accounts. Launch a server →

There are four methods to add elements to a List in Python.

  • append(): append the element to the end of the list.
  • insert(): inserts the element before the given index.
  • extend(): extends the list by appending elements from the iterable.
  • List Concatenation: We can use the + operator to concatenate multiple lists and create a new list.

Prerequisites for the List in Python

In order to complete this tutorial, you will need:

  • Familiarity with installing Python 3. And familiarity with coding in Python.
  • This tutorial was tested with Python 3.9.6.

append()

This function adds a single element to the end of the list.

Python
fruit_list = ["Apple", "Banana"]

print(f'Current Fruits List {fruit_list}')

new_fruit = input("Please enter a fruit name:\n")

fruit_list.append(new_fruit)

print(f'Updated Fruits List {fruit_list}')

Output:

Python
Current Fruits List ['Apple', 'Banana']
Please enter a fruit name:
Orange
Updated Fruits List ['Apple', 'Banana', 'Orange']

This example added Orange to the end of the list.

insert()

This function adds an element at the given index of the list.

Python
num_list = [1, 2, 3, 4, 5]

print(f'Current Numbers List {num_list}')

num = int(input("Please enter a number to add to list:\n"))

index = int(input(f'Please enter the index between 0 and {len(num_list) - 1} to add the number:\n'))

num_list.insert(index, num)

print(f'Updated Numbers List {num_list}')

Output:

Python
Current Numbers List [1, 2, 3, 4, 5]
Please enter a number to add to list:
20
Please enter the index between 0 and 4 to add the number:
2
Updated Numbers List [1, 2, 20, 3, 4, 5]

This example added 20 at the index of 2. 20 has been inserted into the list at this index.

extend()

This function adds iterable elements to the list.

Python
extend_list = []

extend_list.extend([1, 2])  # extending list elements

print(extend_list)

extend_list.extend((3, 4))  # extending tuple elements

print(extend_list)

extend_list.extend("ABC")  # extending string elements

print(extend_list)

Output:

Python
[1, 2]
[1, 2, 3, 4]
[1, 2, 3, 4, 'A', 'B', 'C']

This example added a list of [1, 2]. Then it added a tuple of (3, 4). And then it added a string of ABC.

List Concatenation

If you have to concatenate multiple lists in Python, you can use the + operator. This will create a new list, and the original lists will remain unchanged.

Python
evens = [2, 4, 6]
odds = [1, 3, 5]

nums = odds + evens

print(nums)  # [1, 3, 5, 2, 4, 6]

This example added the list of evens to the end of the list of odds. The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.

Conclusion for adding elements in a list of Python

Python provides multiple ways to add elements to a list. We can append an element at the end of the list, and insert an element at the given index. We can also add a list to another list. If you want to concatenate multiple lists, then use the overloaded + operator.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren