How do i add elements to a list in python?

Learn How to Add Elements in List in Python.

In Python, a list is a dynamic array that can hold both homogeneous (all items of the same data type) and heterogeneous (items of different data types) data. The data stored in the list is arranged in order and identified by a unique number or position named index.

How do i add elements to a list in python?

In the above image, we can see that every element from the list fruits can be accessed by a unique index that starts from 0.

A list provides several built-in functions that can be used to execute a wide range of operations on data stored in it. Append(), extend(), insert(), sort(), remove(), pop() and many other methods can be used to operate on lists in Python.

In this tutorial, we will learn how to add elements to a list in Python using built-in functions. According to the requirements, the different ways to add elements to lists are explained here.

Prerequisites: List in Python

Python Adds Elements to List Examples

As mentioned earlier, a list in Python is a dynamic array, meaning that you can easily add new elements to it. You can add an element at any desired position or the end of the list.

The add(), insert(), and extend() methods in Python are used to add elements to a list. We can add elements to a list using the + operator in Python with these methods.

Methods to add elements in List in Python:

Sr. noMethodsDescription
1. append() Append method adds the object at the end of the list.
2. insert() Insert method adds the object at the specified index.
3. extend() Extend method appends elements from other iterables in python like tuples to the list.
4. List concatenation + operator is used to concatenate multiple lists and create a new list.
5. Slicing Adding another list or tuple using slice().

In the following section of this article, we will see all these methods regarding how to add elements to a list in python.

Add an Item to the End: append()

append() method is used to add elements to the end of the list.

# Creating List 
students = ["Harry", "Ron", "John", "Jenny"]
print(f'Student List: {students}')

# Adding element at the end of list using append()
students.append("Luna")
print(f'New Student List: {students}')

Output:

Student List: ['Harry', 'Ron', 'John', 'Jenny']
New Student List: ['Harry', 'Ron', 'John', 'Jenny', 'Luna']

In this code, students.append() adds new element at the end of the original list students

Append takes only one argument, but we can add another list, heterogenous elements or tupples using append().

# Creating List 
stationary = ["Pen", "Pencil", "Eraser"]
print(f'Stationary List: {stationary}')

# Adding element at the end of list using append()
stationary.append(100)
print(f'\nNew Stationary List: {stationary}')

# Apending list in the existing list
stationary.append(["Sheet","Paint","Brush"])
print(f'\nUpdated Stationary List: {stationary}')

# Apending tupple in the existing list
stationary.append({"Notebook, Textbook"})
print(f'\nUpdated Stationary List: {stationary}')

Output:

Stationary List: ['Pen', 'Pencil', 'Eraser']

New Stationary List: ['Pen', 'Pencil', 'Eraser', 100]

Updated Stationary List: ['Pen', 'Pencil', 'Eraser', 100, ['Sheet', 'Paint', 'Brush']]

Updated Stationary List: ['Pen', 'Pencil', 'Eraser', 100, ['Sheet', 'Paint', 'Brush'], {'Notebook, Textbook'}]

Above code illustrates how we can add lists, tuples, and elements with other data-type using the append() method.

Insert an Item at Specified Index: insert()

This method adds an element to the specified position on the list. The index of the list is used to specify the position where new elements are to be added.

Note: The index of the list in python starts from 0. The negative values like -1 mean one position before the end.

# Creating lists
words = ["apple", "ball", "cat", "elephant"]
print(f'\nCurrent words list: {words}')

# Adding element at the specified position in the list
words.insert(3, "dog")
print(f'\nUpdated words list: {words}')

words.insert(-2, "cricket")
print(f'\nUpdated words list: {words}')

Output:

Current words list: ['apple', 'ball', 'cat', 'elephant']

Updated words list: ['apple', 'ball', 'cat', 'dog', 'elephant']

Updated words list: ['apple', 'ball', 'cat', 'cricket', 'dog', 'elephant']

Here, in firts case, we can see that word.insert(3, "dog") inserts the string "dog" at 3rd index position. In the second case, words.insert(-2, "cricket") inserts the string "cricket" at 2nd index position from the end, the starting index from end being -0.

Combine Lists: extend(), + operator

We can add elements of one list to another using the extend() method and + operator. The extend() method and + operator is used to concatenate/combine one list into another.

extend() Method:

extend() method is used to combine all elements of a list or tuple to the end of the original list.

# Creating List using range() 
list1 = list(range(5))
print(f'List: {list1}')

# Adding elements from another list to the end of list1 using extend()
list1.extend([500,600,700])
print(f'\nUpdated List: {list1}')

# Adding elements from a tupple to the end of list1 using extend()
list1.extend({-100,-200,-300})
print(f'\nUpdated List: {list1}')

Output:

List: [0, 1, 2, 3, 4]

Updated List: [0, 1, 2, 3, 4, 500, 600, 700]

Updated List: [0, 1, 2, 3, 4, 500, 600, 700, -200, -300, -100]

The range() function returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and stops before a specified number. list(range(5)) is used to create a list of the first 5 numbers. extend() function is used to add elements from a list to an existing list.

When a string is given as an argument to the extend() method, the string is split into its characters, and each character of the string is added to the original list one by one.

Let's an example of the same.

# Creating a List
list2 = [1,2,3,4,5,6,7,8]
print(f'List: {list2}')

# Adding elements from another list to the end of list2 using extend()
list2.extend([9,10,11])
print(f'\nUpdated List: {list2}')

# Adding charachters from a string to the end of list1 using extend()
list2.extend("Hello!")
print(f'\nUpdated List: {list2}')

Output:

List: [1, 2, 3, 4, 5, 6, 7, 8]

Updated List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Updated List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 'H', 'e', 'l', 'l', 'o', '!']

In this code list2.extend("Hello!") is used to add elements of string in the existing list. extend() method splits the string into characters and adds these characters to the existing list one by one.

List Concatenation using + operator:

We can combine elements of two lists in a single new list i.e. concatenate two lists using the + operator.

Let's see an example of how to add elements in List in python using + operator.

# Creating lists to combine
even_numbers = [0,2,4,6,8,10]
odd_numbers = [1,3,5,7,9]
print(f'Even numbers list: {even_numbers}')
print(f'\nOdd numbers list: {odd_numbers}')

# Concatenating both the above lists using + operator
numbers = even_numbers + odd_numbers
print(f'\nNumbers List: {numbers}')

Output:

Even numbers list: [0, 2, 4, 6, 8, 10]

Odd numbers list: [1, 3, 5, 7, 9]

Numbers List: [0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9]

In the above code, lists even_numbers and odd_numbers are concatenated and the results are displayed in a new list numbers using the + operator.

We can one list into the existing list with the help of += operators.

# Creating lists
fruits = ["Apple", "Banana", "Orange"]
fruits_new = ["Grapes", "Guava", "Kiwi"]
print(f'Fruits list: {fruits}')
print(f'\nNew fruits list: {fruits_new}')

# Concatenating new list into existing lists using += operators
fruits += fruits_new
print(f'\nCombined Fruits List: {fruits}')

Output:

Fruits list: ['Apple', 'Banana', 'Orange']

New fruits list: ['Grapes', 'Guava', 'Kiwi']

Combined Fruits List: ['Apple', 'Banana', 'Orange', 'Grapes', 'Guava', 'Kiwi']

Here, the existing list fruits is appended with the elements of the new list fruits_new using +=. Operators += concatenates two lists but it gives the result in the existing original string.

Adding Another List or Tuple in the Existing List Using slice()

Slicing in python: Python slice() function return slice object. [start:end: step] is the syntax used to do slicing of any sequence in python. It returns a sliced object containing elements in the given range only. It takes starting index and ending index as arguments.

For eg., list[2:5] means to return the slice of the list starting from index 2 and ending at index 5.

Note: Ending index is not inclusive in slice(). It means the element at the ending index will not be included in the output.

Now lets see how to add elements in list in python using slicing.

# Creating list
num_list = [1,5,6,7,8,9]
print(f'Current number list: {num_list}')

# Adding elemnts in the existing list using slicing
num_list[1:1] = [2,3,4]
print(f'Updated number list: {num_list}')

Output:

Current number list: [1, 5, 6, 7, 8, 9]
Updated number list: [1, 2, 3, 4, 5, 6, 7, 8, 9]

In this code, numlist[1:1] adds the elements from the given list in the specified position i.e. starting from 1 and ending at 1.

We can replace the original element in the list if we change the ending index in the slicing. It will replace the original elements with the new elements in the specified index range. Let's understand this with help of an example.

# Creating list
num_list = [1,5,6,7,8,9]
print(current number list: {num_list}')

# Adding elemnts in the existing list using slicing
num_list[1:2] = [2,3,4]
print(f'Updated number list: {num_list}')

Output:

Current number list: [1, 5, 6, 7, 8, 9]
Updated number list: [1, 2, 3, 4, 6, 7, 8, 9]

In the above code, we can see that code block numlist[1:2] adds the new elements in the original list starting from index position 1 but it replaces the elements at the 2nd index position in the num_list.

Conclusion

  • Elements can be added in the list using built-in functions like append(), insert().
  • append() adds the elements at the end of the list, while insert() adds the element at the specified position.
  • extend() function and + operator is used to concatenate elements from one list/tuple into the other.
  • Slicing can be used to add a sequence of elements to the existing list.

List in Python

How do I add elements anywhere to a list in Python?

Methods to Add Items to a List.
insert() – inserts a single element anywhere in the list..
append() – always adds items (strings, numbers, lists) at the end of the list..
extend() – adds iterable items (lists, tuples, strings) to the end of the list..

Can you add objects to a list in Python?

Python provides a method called . append() that you can use to add items to the end of a given list. This method is widely used either to add a single item to the end of a list or to populate a list using a for loop.

How do I add a list of elements to a list?

How to append one list to another list in Python.
Use list. extend() to combine two lists. Use the syntax list1. extend(list2) to combine list1 and list2 ..
Use list. append() to add a list inside of a list. Use the syntax list1. ... .
Other solutions. Use itertools.chain() to combine many lists..

How do I add multiple values to a list in Python?

We can do this in many ways..
append() We can append values to the end of the list. We use the append() method for this. ... .
insert() You can insert values in a list with the insert() method. Here, you specify a value to insert at a specific position. ... .
extend() extend() can add multiple items to a list. Learn by example:.