How to add a value to a list in python

Append Items

To add an item to the end of the list, use the append[] method:

Example

Using the append[] method to append an item:

thislist = ["apple", "banana", "cherry"]
thislist.append["orange"]
print[thislist]

Try it Yourself »

Insert Items

To insert a list item at a specified index, use the insert[] method.

The insert[] method inserts an item at the specified index:

Example

Insert an item as the second position:

thislist = ["apple", "banana", "cherry"]
thislist.insert[1, "orange"]
print[thislist]

Try it Yourself »

Note: As a result of the examples above, the lists will now contain 4 items.

Extend List

To append elements from another list to the current list, use the extend[] method.

Example

Add the elements of tropical to thislist:

thislist = ["apple", "banana", "cherry"]
tropical = ["mango", "pineapple", "papaya"]
thislist.extend[tropical]
print[thislist]

Try it Yourself »

The elements will be added to the end of the list.

Add Any Iterable

The extend[] method does not have to append lists, you can add any iterable object [tuples, sets, dictionaries etc.].

Example

Add elements of a tuple to a list:

thislist = ["apple", "banana", "cherry"]
thistuple = ["kiwi", "orange"]
thislist.extend[thistuple]
print[thislist]

Try it Yourself »


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

Methods to add elements to List in Python

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

  1. append[]: append the object to the end of the list.
  2. insert[]: inserts the object before the given index.
  3. extend[]: extends the list by appending elements from the iterable.
  4. List Concatenation: We can use + operator to concatenate multiple lists and create a new list.

Python add elements to List Examples

We can add an element to the end of the list or at any given index. There are ways to add elements from an iterable to the list. We can also use + operator to concatenate multiple lists to create a new list.

1. append[]

This function add the element to the end of the list.

fruits = ["Apple", "Banana"]

# 1. append[]
print[f'Current Fruits List {fruits}']

f = input["Please enter a fruit name:\n"]
fruits.append[f]

print[f'Updated Fruits List {fruits}']

Output:

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

2. insert[]

This function adds an element at the given index of the list. It’s useful to add an element at the specified index of the list.

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:

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]

3. extend[]

This function append iterable elements to the list. It is useful to append elements from an iterable to the end of the list.

list_num = []
list_num.extend[[1, 2]]  # extending list elements
print[list_num]
list_num.extend[[3, 4]]  # extending tuple elements
print[list_num]
list_num.extend["ABC"]  # extending string elements
print[list_num]

Output:

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

4. List Concatenation

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

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

nums = odds + evens
print[nums]  # [1, 3, 5, 2, 4, 6]

The new list will contain elements from the list from left to right. It’s similar to the string concatenation in Python.

Conclusion

It’s very easy to add elements to a List in Python programming. We can append an element at the end of the list, 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. References:

  • Python List
  • Python.org Docs

This article describes how to add to a list in Python.

You can add an item [element] to a list with append[] and insert[], and add another list to a list with extend[], +, +=, and slice.

  • Add an item to a list: append[]
  • Add another list to a list [= combine lists]: extend[], +, +=
  • Insert an item into a list: insert[]
  • Insert another list into a list: slice

See the following article on how to remove an item from a list.

  • Remove an item from a list in Python [clear, pop, remove, del]

Add an item to a list: append[]

You can add an item to a list with the append[] method.

A new item is added at the end. If you want to add to other positions, such as the beginning, use the insert[] method described later.

l = [0, 1, 2]

l.append[100]
print[l]
# [0, 1, 2, 100]

l.append['abc']
print[l]
# [0, 1, 2, 100, 'abc']

A list is also added as one item, not combined.

l.append[[3, 4, 5]]
print[l]
# [0, 1, 2, 100, 'abc', [3, 4, 5]]

Add another list to a list [= combine lists]: extend[], +, +=

You can use the extend[] method to add another list to a list, i.e., combine lists. All items are added to the end of the original list.

You may specify other iterable objects, such as tuple.

l = [0, 1, 2]

l.extend[[10, 11, 12]]
print[l]
# [0, 1, 2, 10, 11, 12]

l.extend[[100, 101, 102]]
print[l]
# [0, 1, 2, 10, 11, 12, 100, 101, 102]

In the case of a string [str], each character is added one by one.

l.extend['abc']
print[l]
# [0, 1, 2, 10, 11, 12, 100, 101, 102, 'a', 'b', 'c']

You can also combine lists with the + operator.

In the case of the + operator, a new list is returned. You can also add another list to the existing list with the += operator.

l_new = l + [3, 4, 5]
print[l_new]
# [0, 1, 2, 10, 11, 12, 100, 101, 102, 'a', 'b', 'c', 3, 4, 5]

l += [3, 4, 5]
print[l]
# [0, 1, 2, 10, 11, 12, 100, 101, 102, 'a', 'b', 'c', 3, 4, 5]

Insert an item into a list: insert[]

You can insert an item at any index [position] with the insert[] method.

Set the index for the first parameter and the item to be inserted for the second parameter. The index at the beginning is 0 [zero-based indexing]. For negative values, -1 means one before the end.

l = ['a', 'b', 'c']

l.insert[1, 100]
print[l]
# ['a', 100, 'b', 'c']

l.insert[0, 200]
print[l]
# [200, 'a', 100, 'b', 'c']

l.insert[-1, 300]
print[l]
# [200, 'a', 100, 'b', 300, 'c']

l.insert[-2, 400]
print[l]
# [200, 'a', 100, 'b', 400, 300, 'c']

Like append[], the list is added as a single item, not combined.

l.insert[0, [-1, -2, -3]]
print[l]
# [[-1, -2, -3], 200, 'a', 100, 'b', 400, 300, 'c']

Note that insert[] is an O[n] operation and is inefficient. See the official wiki for the computational complexity of various operations on list.

  • TimeComplexity - Python Wiki

The deque type is provided in the standard library collections module to add an item to the head with O[1]. For example, if you want to treat data as a queue [FIFO], it is more efficient to use deque.

  • Deque with collections.deque in Python

Insert another list into a list: slice

If you specify a range using slice and assign another list or tuple, all items are added.

l = ['a', 'b', 'c']

l[1:1] = [100, 200, 300]
print[l]
# ['a', 100, 200, 300, 'b', 'c']

You can also replace the original item. All items in the specified range are replaced.

l = ['a', 'b', 'c']

l[1:2] = [100, 200, 300]
print[l]
# ['a', 100, 200, 300, 'c']

See the following article for details on slicing.

  • How to slice a list, string, tuple in Python

How do you add a value to a list?

Add another list to a list [= combine lists]: extend[] , + , += You can use the extend[] method to add another list to a list, i.e., combine lists. All items are added to the end of the original list. You may specify other iterable objects, such as tuple .

Can you insert into a list Python?

There are three methods we can use when adding an item to a list in Python. They are: insert[] , append[] , and extend[] .

Chủ Đề