AppDividend
Latest Code Tutorials

Python Add To List Example | Add Element In Python List

0

Python add to list example is today’s topic. Python list is an object. Python has a built-in list type named “list“. List literals are written within square brackets [ ]. Adding or removing the item from the list are common operations you can think of. Python provides an in-built function called list append(), which inserts the item at the end of the list.

Python add to list

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

We can add elements in a list using two ways.

  1. Python List append()
  2. Python List extend()

Python 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. See the following syntax.

list.append(element)

The method takes a single argument.

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

Adding List to List in Python

Let’s see how to add list to 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.

Python 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.

See the following syntax.

list.extend(iterable)

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.

Finally, we have seen all the methods to add elements to the list.

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.