Python Add to List: The Complete Guide

Python list is a built-in Object. List literals are written within square brackets [ ]. Adding or removing the item from the List are regular operations you can think of. Python provides built-in functions like append(), which inserts an element at the end of the List, and many more, which we will talk about in a minute.

Python add to List

To add elements to a list in Python, use the list.append() method. The list.append() method is a built-in method that appends an element to the end of the list. There are other ways to add elements to the Python list,

  1. List extend(): It extends the List by appending elements from the iterable.
  2. List insert(): It inserts the object before the given index.
  3. List concatenation: We can use the + operator to concatenate multiple lists and create a new list.

Lists work similarly to strings and use the len() function and square brackets [ ] to access data, with the first element at index 0.

List append()

Python list append() method takes an argument as a single element and adds it at the end of a list.

The length of a list increases by one.

Syntax

list.append(element)

The method takes a single argument.

Parameters

element: An element to be added at the end of the List

The element can be numbers, strings, dictionaries, another list, and so on.

The method doesn’t return any value (returns None).

See the following code example.

# app.py

mando = ['Mandalorian', 'Baby Yoda', 'Cara Dune']
mando.append('Atst')
print('Updated mando list: ', mando)

Output

➜  pyt python3 app.py
Updated mando list:  ['Mandalorian', 'Baby Yoda', 'Cara Dune', 'Atst']
➜  pyt

How to add List into List

Let’s see how to add the List to the List in Python.

# app.py

mando = ['Mandalorian', 'Baby Yoda', 'Cara Dune']
starwars = ['c-3po', 'ray']
mando.append(starwars)
print('Updated mando list: ', mando)

Output

➜  pyt python3 app.py
Updated mando list:  ['Mandalorian', 'Baby Yoda', 'Cara Dune', ['c-3po', 'ray']]
➜  pyt

From the output, you can see that a new list is added as an item to the mando list.

List extend()

Python list extend() method inserts the specified list items (or any iterable) to the end of the current List. The extend() extends the List by adding all elements of the List (passed as an argument) to an end.

Unlike Python append(), it adds items individually.

Syntax

list.extend(iterable)

Parameters

The extend() function takes only one parameter, which is iterable like List, tuple, dict, or set.

See the following code example.

# app.py

mando = ['Mandalorian', 'Baby Yoda', 'Cara Dune']
starwars = ['c-3po', 'ray']
mando.extend(starwars)
print('Updated mando list: ', mando)

Output

➜  pyt python3 app.py
Updated mando list:  ['Mandalorian', 'Baby Yoda', 'Cara Dune', 'c-3po', 'ray']
➜  pyt

From the output, you can see that the extend() method did not append the whole List; instead, it adds an individual item of that list at the end of the List.

Time Complexity:

Append has constant time complexity, i.e., O(1).
Extend has the time complexity of O(k), where k is the length of the List, which needs to be added.

List insert()

Python list insert() is a built-in Python function that adds an element to the given index of the List. To add an element to the specified index of the List, then the insert() method of Python is handy.

See the following code.

num_list = [11, 21, 31, 41, 51, 61, 71, 81]

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

Current Numbers List [11, 21, 31, 41, 51, 61, 71, 81]
Please enter a number to add to list:
19
Please enter the index between 0 and 7 to add the number:
7
Updated Numbers List [11, 21, 31, 41, 51, 61, 71, 19, 81]

In the above example, first, we have defined a list and then use the input() function to get the user input.

We have to get two inputs from the user because the insert() function takes two parameters.

  1. Element, which you have to add.
  2. The index position to add that element.

After getting the inputs, we update the List using the insert() function and print the List.

List Concatenation

If you have to concatenate multiple lists, then you can use the “+” operator. This operation will create the new List, and the original List will remain unchanged.

So, this way is a kind of Pure function in functional programming.

See the following code.

langsA = ['Python', 'PHP', 'Javascript']
langsB = ['Golang', 'Java', 'Dart']

combo = langsA + langsB
print(combo)

Output

['Python', 'PHP', 'Javascript', 'Golang', 'Java', 'Dart']

The + operator just concatenates the two lists and makes one List.

That is it for this tutorial.

Related Posts

How to convert Python list to json

Python list copy()

Python list reverse()

Python list sort()

Python list pop()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.