AppDividend
Latest Code Tutorials

Python Add to List: How to Add Elements in Python List

0

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

Python add to List

To add elements in the Python list, we can use one of the following four methods.

  1. List append(): It appends an item at the end of the List.
  2. List extend(): It extends the List by appending elements from the iterable.
  3. List insert(): It inserts the object before the given index.
  4. List concatenation: We can use + 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.

Let’s see one by one method.

List append()

Python list append() method takes an argument as a single element and adds 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.

Python extend() extends the List by adding all items 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()

The list insert() function adds an item at the given index of the List. If you want to add an element at 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 are updating the List using 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 concatenate the two lists and make 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 Reply

Your email address will not be published.

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