How to Add Elements to a List in Python (append, extend and insert)

How to Add Elements to a List in Python (append, extend and insert)

The Python list data type has three methods for adding elements: append(), extend(), and insert(). How to Add Elements to a List in Python (append, extend and insert)

How to Add Elements to a List in Python (append, extend and insert)

Posted Jun 11, 2020

3 min read

When working with lists in Python, you will often want to add new elements to the list.

The Python list data type has three methods for adding elements:

  • append() - appends a single element to the list.
  • extend() - appends elements of an iterable to the list.
  • insert() - inserts a single item at a given position of the list.

All three methods modify the list in place and return None.

Python List append()

The append() method adds a single element to the end of the list .

The syntax of the append() method is as follows:

list.append(element) 

Copy

Where, element is the element to be added to the list.

Here is an example:characters = ['Tokyo', 'Lisbon', 'Moscow', 'Berlin'] 

characters.append('Nairobi')

print('Updated list:', characters)

Copy

Updated list: ['Tokyo', 'Lisbon', 'Moscow', 'Berlin', 'Nairobi']

The element parameter can be an object of any data type:

odd_numbers = [1, 3, 5, 7] 

even_numbers = [2, 4, 6]

odd_numbers.append(even_numbers)

print('Updated list:', odd_numbers)

python add elements append extend

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python list extend: How to Add Element in the List

Python list extend() is an inbuilt function that adds the specified list elements (or any iterable) to the end of the current list.

Python Add To Dictionary Example | Add Key In Python Dict

In this tutorial, we will see Python Add To Dictionary Example. We will see how to add element or item into Python dictionary using not in, if-else method.

Python any: How to Check If Element is Iterable or Not

Python any() function returns True if any element of an iterable is True otherwise any() function returns False. The syntax is any().

Python Tricks Every Developer Should Know

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

How to Remove all Duplicate Files on your Drive via Python

Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.