Skip to content

List Operations

List is a mutable data type. This means that you can change the list after it has been created. In this tutorial, you will learn how to change list items in Python. You can change a list item at a specific index, change multiple list items at once, or change a list item to a different type.

Change List Item at Specific Index

You can change a list item at a specific index by assigning a new value to the index. For example, you can change the first item in the list to a different value by assigning a new value to the index 0.

syntax.py
list[index] = new_value
syntax.py
list[index] = new_value

The following example shows how to change the first item in the list to a different value.

change_first_item.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers[0] = 10
print(numbers)
change_first_item.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers[0] = 10
print(numbers)

Output:

command
C:\Users\username>python change_first_item.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]
command
C:\Users\username>python change_first_item.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]

In the example above, we changed the first item in the list to 10. The rest of the items in the list remain the same.

Change Multiple List Items

You can change multiple list items at once by assigning a new list to a slice of the list. For example, you can change the first three items in the list to different values by assigning a new list to the slice 0:30:3.

syntax.py
list[start:end] = new_list
syntax.py
list[start:end] = new_list

The following example shows how to change the first three items in the list to different values.

change_multiple_items.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers[0:3] = [10, 20, 30]
print(numbers)
change_multiple_items.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers[0:3] = [10, 20, 30]
print(numbers)

Output:

command
C:\Users\username>python change_multiple_items.py
[1, 2, 3, 4, 5]
[10, 20, 30, 4, 5]
command
C:\Users\username>python change_multiple_items.py
[1, 2, 3, 4, 5]
[10, 20, 30, 4, 5]

In the example above, we changed the first three items in the list to 10, 20, and 30. The rest of the items in the list remain the same.

Change List Item to Different Type

You can change a list item to a different type by assigning a new value to the index. For example, you can change the first item in the list to a string by assigning a string to the index 0.

syntax.py
list[index] = new_value
syntax.py
list[index] = new_value

The following example shows how to change the first item in the list to a string.

change_item_type.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers[0] = "one"
print(numbers)
change_item_type.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers[0] = "one"
print(numbers)

Output:

command
C:\Users\username>python change_item_type.py
[1, 2, 3, 4, 5]
['one', 2, 3, 4, 5]
command
C:\Users\username>python change_item_type.py
[1, 2, 3, 4, 5]
['one', 2, 3, 4, 5]

In the example above, we changed the first item in the list to a string. The rest of the items in the list remain the same.

Insert List Item

You can insert a new list item at a specific index by using the insert()insert() method. The insert()insert() method takes two arguments: the index where you want to insert the new item and the new item.

syntax.py
list.insert(index, new_item)
syntax.py
list.insert(index, new_item)

The following example shows how to insert a new item at the beginning of the list.

insert_item.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.insert(0, 10)
print(numbers)
insert_item.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.insert(0, 10)
print(numbers)

Output:

command
C:\Users\username>python insert_item.py
[1, 2, 3, 4, 5]
[10, 1, 2, 3, 4, 5]
command
C:\Users\username>python insert_item.py
[1, 2, 3, 4, 5]
[10, 1, 2, 3, 4, 5]

In the example above, we inserted a new item at the beginning of the list. The rest of the items in the list were shifted to the right.

Append List Item

You can append a new list item to the end of the list by using the append()append() method. The append()append() method takes one argument: the new item.

syntax.py
list.append(new_item)
syntax.py
list.append(new_item)

The following example shows how to append a new item to the end of the list.

append_item.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.append(10)
print(numbers)
append_item.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.append(10)
print(numbers)

Output:

command
C:\Users\username>python append_item.py
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 10]
command
C:\Users\username>python append_item.py
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 10]

In the example above, we appended a new item to the end of the list.

Insert Item at Negative Index

You can insert a new list item at a negative index by using the insert()insert() method. The insert()insert() method takes two arguments: the index where you want to insert the new item and the new item.

syntax.py
list.insert(index, new_item)
syntax.py
list.insert(index, new_item)

The following example shows how to insert a new item at the end of the list.

insert_item_at_negative_index.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.insert(-1, 10)
print(numbers)
insert_item_at_negative_index.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.insert(-1, 10)
print(numbers)

Output:

command
C:\Users\username>python insert_item_at_negative_index.py
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 10, 5]
command
C:\Users\username>python insert_item_at_negative_index.py
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 10, 5]

In the example above, we inserted a new item at the end of the list. The rest of the items in the list were shifted to the right.

Remove List Item

You can remove a list item by using the remove()remove() method. The remove()remove() method takes one argument: the item you want to remove.

syntax.py
list.remove(item)
syntax.py
list.remove(item)

The following example shows how to remove an item from the list.

remove_item.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.remove(3)
print(numbers)
remove_item.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.remove(3)
print(numbers)

Output:

command
C:\Users\username>python remove_item.py
[1, 2, 3, 4, 5]
[1, 2, 4, 5]
command
C:\Users\username>python remove_item.py
[1, 2, 3, 4, 5]
[1, 2, 4, 5]

In the example above, we removed the item 3 from the list.

Remove List Item at Specific Index

You can remove a list item at a specific index by using the pop()pop() method. The pop()pop() method takes one argument: the index of the item you want to remove.

syntax.py
list.pop(index)
syntax.py
list.pop(index)

The following example shows how to remove an item at a specific index from the list.

remove_item_at_index.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.pop(3)
print(numbers)
remove_item_at_index.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.pop(3)
print(numbers)

Output:

command
C:\Users\username>python remove_item_at_index.py
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
command
C:\Users\username>python remove_item_at_index.py
[1, 2, 3, 4, 5]
[1, 2, 3, 5]

In the example above, we removed the item at index 3 from the list.

Remove All List Items

You can remove all list items by using the clear()clear() method. The clear()clear() method takes no arguments.

syntax.py
list.clear()
syntax.py
list.clear()

The following example shows how to remove all items from the list.

remove_all_items.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.clear()
print(numbers)
remove_all_items.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers.clear()
print(numbers)

Output:

command
C:\Users\username>python remove_all_items.py
[1, 2, 3, 4, 5]
[]
command
C:\Users\username>python remove_all_items.py
[1, 2, 3, 4, 5]
[]

In the example above, we removed all items from the list.

Remove List Item using del

You can remove a list item by using the deldel keyword. The deldel keyword takes one argument: the item you want to remove.

syntax.py
del list[index]
syntax.py
del list[index]

The following example shows how to remove an item from the list.

remove_item_using_del.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
del numbers[3]
print(numbers)
remove_item_using_del.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
del numbers[3]
print(numbers)

Output:

command
C:\Users\username>python remove_item_using_del.py
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
command
C:\Users\username>python remove_item_using_del.py
[1, 2, 3, 4, 5]
[1, 2, 3, 5]

In the example above, we removed the item 4 from the list.

Remove List Item using del with Slice

You can remove multiple list items by using the deldel keyword with a slice. The deldel keyword takes one argument: the slice you want to remove.

syntax.py
del list[start:end]
syntax.py
del list[start:end]

The following example shows how to remove multiple items from the list.

remove_multiple_items_using_del.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
del numbers[1:3]
print(numbers)
remove_multiple_items_using_del.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
del numbers[1:3]
print(numbers)

Output:

command
C:\Users\username>python remove_multiple_items_using_del.py
[1, 2, 3, 4, 5]
[1, 4, 5]
command
C:\Users\username>python remove_multiple_items_using_del.py
[1, 2, 3, 4, 5]
[1, 4, 5]

In the example above, we removed the items 2 and 3 from the list.

Change List Item in Loop

You can change list items in a loop by using the enumerate()enumerate() function. The enumerate()enumerate() function takes one argument: the list you want to loop over. The enumerate()enumerate() function returns a list of tuples. Each tuple contains the index and the item at that index.

syntax.py
for index, item in enumerate(list):
    # do something with index and item
syntax.py
for index, item in enumerate(list):
    # do something with index and item

The following example shows how to change list items in a loop.

change_items_in_loop.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
for index, item in enumerate(numbers):
    numbers[index] = item * 10
print(numbers)
change_items_in_loop.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
for index, item in enumerate(numbers):
    numbers[index] = item * 10
print(numbers)

Output:

command
C:\Users\username>python change_items_in_loop.py
[1, 2, 3, 4, 5]
[10, 20, 30, 40, 50]
command
C:\Users\username>python change_items_in_loop.py
[1, 2, 3, 4, 5]
[10, 20, 30, 40, 50]

In the example above, we changed all items in the list to 10 times their original value.

Change List Item in List Comprehension

You can change list items in a list comprehension by using the enumerate()enumerate() function. The enumerate()enumerate() function takes one argument: the list you want to loop over. The enumerate()enumerate() function returns a list of tuples. Each tuple contains the index and the item at that index.

syntax.py
[expression for index, item in enumerate(list)]
syntax.py
[expression for index, item in enumerate(list)]

The following example shows how to change list items in a list comprehension.

change_items_in_list_comprehension.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers = [item * 10 for index, item in enumerate(numbers)]
print(numbers)
change_items_in_list_comprehension.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers = [item * 10 for index, item in enumerate(numbers)]
print(numbers)

Output:

command
C:\Users\username>python change_items_in_list_comprehension.py
[1, 2, 3, 4, 5]
[10, 20, 30, 40, 50]
command
C:\Users\username>python change_items_in_list_comprehension.py
[1, 2, 3, 4, 5]
[10, 20, 30, 40, 50]

In the example above, we changed all items in the list to 10 times their original value.

Change List Item in List Comprehension with Condition

You can change list items in a list comprehension with a condition by using the enumerate()enumerate() function. The enumerate()enumerate() function takes one argument: the list you want to loop over. The enumerate()enumerate() function returns a list of tuples. Each tuple contains the index and the item at that index.

syntax.py
[expression for index, item in enumerate(list) if condition]
syntax.py
[expression for index, item in enumerate(list) if condition]

The following example shows how to change list items in a list comprehension with a condition.

change_items_in_list_comprehension_with_condition.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers = [item * 10 for index, item in enumerate(numbers) if item % 2 == 0]
print(numbers)
change_items_in_list_comprehension_with_condition.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers = [item * 10 for index, item in enumerate(numbers) if item % 2 == 0]
print(numbers)

Output:

command
C:\Users\username>python change_items_in_list_comprehension_with_condition.py
[1, 2, 3, 4, 5]
[20, 40]
command
C:\Users\username>python change_items_in_list_comprehension_with_condition.py
[1, 2, 3, 4, 5]
[20, 40]

In the example above, we changed all even items in the list to 10 times their original value.

Sort List

You can sort a list by using the sort()sort() method. The sort()sort() method takes no arguments.

syntax.py
list.sort()
syntax.py
list.sort()

The following example shows how to sort a list.

sort_list.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers.sort()
print(numbers)
sort_list.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers.sort()
print(numbers)

Output:

command
C:\Users\username>python sort_list.py
[5, 2, 4, 1, 3]
[1, 2, 3, 4, 5]
command
C:\Users\username>python sort_list.py
[5, 2, 4, 1, 3]
[1, 2, 3, 4, 5]

In the example above, we sorted the list in ascending order.

Sort List in Descending Order

You can sort a list in descending order by using the sort()sort() method with the argument reverse=Truereverse=True.

syntax.py
list.sort(reverse=True)
syntax.py
list.sort(reverse=True)

The following example shows how to sort a list in descending order.

sort_list_in_descending_order.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers.sort(reverse=True)
print(numbers)
sort_list_in_descending_order.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers.sort(reverse=True)
print(numbers)

Output:

command
C:\Users\username>python sort_list_in_descending_order.py
[5, 2, 4, 1, 3]
[5, 4, 3, 2, 1]
command
C:\Users\username>python sort_list_in_descending_order.py
[5, 2, 4, 1, 3]
[5, 4, 3, 2, 1]

In the example above, we sorted the list in descending order.

Sort List using Sorted

You can sort a list by using the sorted()sorted() function. The sorted()sorted() function takes one argument: the list you want to sort.

syntax.py
sorted(list)
syntax.py
sorted(list)

The following example shows how to sort a list.

sort_list_using_sorted.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers = sorted(numbers)
print(numbers)
sort_list_using_sorted.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers = sorted(numbers)
print(numbers)

Output:

command
C:\Users\username>python sort_list_using_sorted.py
[5, 2, 4, 1, 3]
[1, 2, 3, 4, 5]
command
C:\Users\username>python sort_list_using_sorted.py
[5, 2, 4, 1, 3]
[1, 2, 3, 4, 5]

In the example above, we sorted the list in ascending order.

Sort List using Sorted in Descending Order

You can sort a list in descending order by using the sorted()sorted() function with the argument reverse=Truereverse=True.

syntax.py
sorted(list, reverse=True)
syntax.py
sorted(list, reverse=True)

The following example shows how to sort a list in descending order.

sort_list_using_sorted_in_descending_order.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers = sorted(numbers, reverse=True)
print(numbers)
sort_list_using_sorted_in_descending_order.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers = sorted(numbers, reverse=True)
print(numbers)

Output:

command
C:\Users\username>python sort_list_using_sorted_in_descending_order.py
[5, 2, 4, 1, 3]
[5, 4, 3, 2, 1]
command
C:\Users\username>python sort_list_using_sorted_in_descending_order.py
[5, 2, 4, 1, 3]
[5, 4, 3, 2, 1]

In the example above, we sorted the list in descending order.

Sort List with Key

You can sort a list with a key by using the sort()sort() method with the argument keykey. The sort()sort() method takes one argument: the key function.

syntax.py
list.sort(key=key_function)
syntax.py
list.sort(key=key_function)

The following example shows how to sort a list with a key.

sort_list_with_key.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers.sort(key=lambda x: x % 2 == 0)
print(numbers)
sort_list_with_key.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers.sort(key=lambda x: x % 2 == 0)
print(numbers)

Output:

command
C:\Users\username>python sort_list_with_key.py
[5, 2, 4, 1, 3]
[5, 1, 3, 2, 4]
command
C:\Users\username>python sort_list_with_key.py
[5, 2, 4, 1, 3]
[5, 1, 3, 2, 4]

In the example above, we sorted the list with a key. The key function returns TrueTrue if the item is even and FalseFalse if the item is odd.

Sort List with Key in Descending Order

You can sort a list with a key in descending order by using the sort()sort() method with the argument keykey and reverse=Truereverse=True. The sort()sort() method takes two arguments: the key function and the reverse flag.

syntax.py
list.sort(key=key_function, reverse=True)
syntax.py
list.sort(key=key_function, reverse=True)

The following example shows how to sort a list with a key in descending order.

sort_list_with_key_in_descending_order.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers.sort(key=lambda x: x % 2 == 0, reverse=True)
print(numbers)
sort_list_with_key_in_descending_order.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers.sort(key=lambda x: x % 2 == 0, reverse=True)
print(numbers)

Output:

command
C:\Users\username>python sort_list_with_key_in_descending_order.py
[5, 2, 4, 1, 3]
[4, 2, 5, 1, 3]
command
C:\Users\username>python sort_list_with_key_in_descending_order.py
[5, 2, 4, 1, 3]
[4, 2, 5, 1, 3]

In the example above, we sorted the list with a key in descending order. The key function returns TrueTrue if the item is even and FalseFalse if the item is odd.

Sort List with Key using Sorted

You can sort a list with a key by using the sorted()sorted() function with the argument keykey. The sorted()sorted() function takes two arguments: the list you want to sort and the key function.

syntax.py
sorted(list, key=key_function)
syntax.py
sorted(list, key=key_function)

The following example shows how to sort a list with a key.

sort_list_with_key_using_sorted.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers = sorted(numbers, key=lambda x: x % 2 == 0)
print(numbers)
sort_list_with_key_using_sorted.py
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers = sorted(numbers, key=lambda x: x % 2 == 0)
print(numbers)

Output:

command
C:\Users\username>python sort_list_with_key_using_sorted.py
[5, 2, 4, 1, 3]
[5, 1, 3, 2, 4]
command
C:\Users\username>python sort_list_with_key_using_sorted.py
[5, 2, 4, 1, 3]
[5, 1, 3, 2, 4]

In the example above, we sorted the list with a key. The key function returns TrueTrue if the item is even and FalseFalse if the item is odd.

Copy List

You can copy a list using == operator. The == operator takes one argument: the list you want to copy.

syntax.py
new_list = list
syntax.py
new_list = list

The following example shows how to copy a list.

copy_list.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
new_numbers = numbers
print(new_numbers)
copy_list.py
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
new_numbers = numbers
print(new_numbers)

Output:

command
C:\Users\username>python copy_list.py
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
command
C:\Users\username>python copy_list.py
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]

In the example above, we copied the list. The new list is a reference to the original list. If you change the original list, the new list will also change.

Copy List using Copy

You can copy a list using the copy()copy() method. The copy()copy() method takes no arguments.

syntax.py
new_list = list.copy()
syntax.py
new_list = list.copy()

The following example shows how to copy a list.

copy_list_using_copy.py
numbers = [1, 2, 3, 4, 5]
 
new_numbers = numbers.copy()
new_numbers[0] = 10
 
print(numbers)
print(new_numbers)
copy_list_using_copy.py
numbers = [1, 2, 3, 4, 5]
 
new_numbers = numbers.copy()
new_numbers[0] = 10
 
print(numbers)
print(new_numbers)

Output:

command
C:\Users\username>python copy_list_using_copy.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]
command
C:\Users\username>python copy_list_using_copy.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]

In the example above, we copied the list. The new list is a copy of the original list. If you change the original list, the new list will not change.

Copy List using List

You can copy a list using the list()list() function. The list()list() function takes one argument: the list you want to copy.

syntax.py
new_list = list(list)
syntax.py
new_list = list(list)

The following example shows how to copy a list.

copy_list_using_list.py
numbers = [1, 2, 3, 4, 5]
 
new_numbers = list(numbers)
new_numbers[0] = 10
 
print(numbers)
print(new_numbers)
copy_list_using_list.py
numbers = [1, 2, 3, 4, 5]
 
new_numbers = list(numbers)
new_numbers[0] = 10
 
print(numbers)
print(new_numbers)

Output:

command
C:\Users\username>python copy_list_using_list.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]
command
C:\Users\username>python copy_list_using_list.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]

In the example above, we copied the list. The new list is a copy of the original list. If you change the original list, the new list will not change.

Copy List using Slicing

You can copy a list using slicing. The slicing syntax is list[:]list[:].

syntax.py
new_list = list[:]
syntax.py
new_list = list[:]

The following example shows how to copy a list.

copy_list_using_slicing.py
numbers = [1, 2, 3, 4, 5]
 
new_numbers = numbers[:]
new_numbers[0] = 10
 
print(numbers)
print(new_numbers)
copy_list_using_slicing.py
numbers = [1, 2, 3, 4, 5]
 
new_numbers = numbers[:]
new_numbers[0] = 10
 
print(numbers)
print(new_numbers)

Output:

command
C:\Users\username>python copy_list_using_slicing.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]
command
C:\Users\username>python copy_list_using_slicing.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]

In the example above, we copied the list. The new list is a copy of the original list. If you change the original list, the new list will not change.

Copy List using List Comprehension

You can copy a list using list comprehension. The list comprehension syntax is [item for item in list][item for item in list].

syntax.py
new_list = [item for item in list]
syntax.py
new_list = [item for item in list]

The following example shows how to copy a list.

copy_list_using_list_comprehension.py
numbers = [1, 2, 3, 4, 5]
 
new_numbers = [item for item in numbers]
new_numbers[0] = 10
 
print(numbers)
print(new_numbers)
copy_list_using_list_comprehension.py
numbers = [1, 2, 3, 4, 5]
 
new_numbers = [item for item in numbers]
new_numbers[0] = 10
 
print(numbers)
print(new_numbers)

Output:

command
C:\Users\username>python copy_list_using_list_comprehension.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]
command
C:\Users\username>python copy_list_using_list_comprehension.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]

In the example above, we copied the list. The new list is a copy of the original list. If you change the original list, the new list will not change.

Copy List using Deepcopy

You can copy a list using the deepcopy()deepcopy() function from the copycopy module. The deepcopy()deepcopy() function takes one argument: the list you want to copy.

syntax.py
new_list = deepcopy(list)
syntax.py
new_list = deepcopy(list)

The following example shows how to copy a list.

copy_list_using_deepcopy.py
from copy import deepcopy
 
numbers = [1, 2, 3, 4, 5]
 
new_numbers = deepcopy(numbers)
new_numbers[0] = 10
 
print(numbers)
print(new_numbers)
copy_list_using_deepcopy.py
from copy import deepcopy
 
numbers = [1, 2, 3, 4, 5]
 
new_numbers = deepcopy(numbers)
new_numbers[0] = 10
 
print(numbers)
print(new_numbers)

Output:

command
C:\Users\username>python copy_list_using_deepcopy.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]
command
C:\Users\username>python copy_list_using_deepcopy.py
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]

In the example above, we copied the list. The new list is a copy of the original list. If you change the original list, the new list will not change.

Join List

You can join two lists by using the ++ operator. The ++ operator takes two arguments: the first list and the second list.

syntax.py
new_list = list1 + list2
syntax.py
new_list = list1 + list2

The following example shows how to join two lists.

join_list.py
numbers1 = [1, 2, 3, 4, 5]
 
numbers2 = [6, 7, 8, 9, 10]
numbers = numbers1 + numbers2
 
print(numbers1)
print(numbers2)
print(numbers)
join_list.py
numbers1 = [1, 2, 3, 4, 5]
 
numbers2 = [6, 7, 8, 9, 10]
numbers = numbers1 + numbers2
 
print(numbers1)
print(numbers2)
print(numbers)

Output:

command
C:\Users\username>python join_list.py
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
command
C:\Users\username>python join_list.py
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In the example above, we joined two lists.

Join List using Extend

You can join two lists by using the extend()extend() method. The extend()extend() method takes one argument: the list you want to join.

syntax.py
list1.extend(list2)
syntax.py
list1.extend(list2)

The following example shows how to join two lists.

join_list_using_extend.py
numbers1 = [1, 2, 3, 4, 5]
 
numbers2 = [6, 7, 8, 9, 10]
numbers1.extend(numbers2)
 
print(numbers1)
print(numbers2)
join_list_using_extend.py
numbers1 = [1, 2, 3, 4, 5]
 
numbers2 = [6, 7, 8, 9, 10]
numbers1.extend(numbers2)
 
print(numbers1)
print(numbers2)

Output:

command
C:\Users\username>python join_list_using_extend.py
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[6, 7, 8, 9, 10]
command
C:\Users\username>python join_list_using_extend.py
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[6, 7, 8, 9, 10]

In the example above, we joined two lists.

Join List using Append

You can join two lists by using the append()append() method. The append()append() method takes one argument: the list you want to join.

syntax.py
list1.append(list2)
syntax.py
list1.append(list2)

The following example shows how to join two lists.

join_list_using_append.py
numbers1 = [1, 2, 3, 4, 5]
 
numbers2 = [6, 7, 8, 9, 10]
numbers1.append(numbers2)
 
print(numbers1)
print(numbers2)
join_list_using_append.py
numbers1 = [1, 2, 3, 4, 5]
 
numbers2 = [6, 7, 8, 9, 10]
numbers1.append(numbers2)
 
print(numbers1)
print(numbers2)

Output:

command
C:\Users\username>python join_list_using_append.py
[1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]
[6, 7, 8, 9, 10]
command
C:\Users\username>python join_list_using_append.py
[1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]
[6, 7, 8, 9, 10]

In the example above, we joined two lists.

Join List using Insert

You can join two lists by using the insert()insert() method. The insert()insert() method takes two arguments: the index where you want to insert the new list and the new list.

syntax.py
list1.insert(index, list2)
syntax.py
list1.insert(index, list2)

The following example shows how to join two lists.

join_list_using_insert.py
numbers1 = [1, 2, 3, 4, 5]
 
numbers2 = [6, 7, 8, 9, 10]
numbers1.insert(5, numbers2)
 
print(numbers1)
print(numbers2)
join_list_using_insert.py
numbers1 = [1, 2, 3, 4, 5]
 
numbers2 = [6, 7, 8, 9, 10]
numbers1.insert(5, numbers2)
 
print(numbers1)
print(numbers2)

Output:

command
C:\Users\username>python join_list_using_insert.py
[1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]
[6, 7, 8, 9, 10]
command
C:\Users\username>python join_list_using_insert.py
[1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]
[6, 7, 8, 9, 10]

In the example above, we joined two lists.

Join List using List Comprehension

You can join two lists by using list comprehension. The list comprehension syntax is [item for item in list1 + list2][item for item in list1 + list2].

syntax.py
new_list = [item for item in list1 + list2]
syntax.py
new_list = [item for item in list1 + list2]

The following example shows how to join two lists.

join_list_using_list_comprehension.py
numbers1 = [1, 2, 3, 4, 5]
 
numbers2 = [6, 7, 8, 9, 10]
numbers = [item for item in numbers1 + numbers2]
 
print(numbers1)
print(numbers2)
print(numbers)
join_list_using_list_comprehension.py
numbers1 = [1, 2, 3, 4, 5]
 
numbers2 = [6, 7, 8, 9, 10]
numbers = [item for item in numbers1 + numbers2]
 
print(numbers1)
print(numbers2)
print(numbers)

Output:

command
C:\Users\username>python join_list_using_list_comprehension.py
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
command
C:\Users\username>python join_list_using_list_comprehension.py
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In the example above, we joined two lists.

Convert List to String

You can convert a list to a string by using the join()join() method. The join()join() method takes one argument: the list you want to convert to a string.

syntax.py
string = ''.join(list)
syntax.py
string = ''.join(list)

The following example shows how to convert a list to a string.

convert_list_to_string.py
numbers = [1, 2, 3, 4, 5]
 
string = ''.join(str(number) for number in numbers)
 
print(numbers)
print(string)
convert_list_to_string.py
numbers = [1, 2, 3, 4, 5]
 
string = ''.join(str(number) for number in numbers)
 
print(numbers)
print(string)

Output:

command
C:\Users\username>python convert_list_to_string.py
[1, 2, 3, 4, 5]
12345
command
C:\Users\username>python convert_list_to_string.py
[1, 2, 3, 4, 5]
12345

In the example above, we converted a list to a string.

You can also convert into string with separator.

convert_list_to_string_with_separator.py
numbers = [1, 2, 3, 4, 5]
 
string = ','.join(str(number) for number in numbers)
 
print(numbers)
print(string)
convert_list_to_string_with_separator.py
numbers = [1, 2, 3, 4, 5]
 
string = ','.join(str(number) for number in numbers)
 
print(numbers)
print(string)

Output:

command
C:\Users\username>python convert_list_to_string_with_separator.py
[1, 2, 3, 4, 5]
1,2,3,4,5
command
C:\Users\username>python convert_list_to_string_with_separator.py
[1, 2, 3, 4, 5]
1,2,3,4,5

In the example above, we converted a list to a string with separator.

You can convert a list to a string by using the map()map() function. The map()map() function takes two arguments: the function you want to apply to each item in the list and the list you want to convert to a string.

syntax.py
string = ''.join(map(function, list))
syntax.py
string = ''.join(map(function, list))

The following example shows how to convert a list to a string.

convert_list_to_string_using_map.py
numbers = [1, 2, 3, 4, 5]
 
string = ''.join(map(str, numbers))
 
print(numbers)
print(string)
convert_list_to_string_using_map.py
numbers = [1, 2, 3, 4, 5]
 
string = ''.join(map(str, numbers))
 
print(numbers)
print(string)

Output:

command
C:\Users\username>python convert_list_to_string_using_map.py
[1, 2, 3, 4, 5]
12345
command
C:\Users\username>python convert_list_to_string_using_map.py
[1, 2, 3, 4, 5]
12345

In the example above, we converted a list to a string.

Conclusion

You can change list items in Python. You can change a list item at a specific index, change multiple list items at once, or change a list item to a different type. You can also insert, append, and remove items from a list. You can change list items in a loop or in a list comprehension. For more learning resources, see Python Central Hub.

Was this page helpful?

Let us know how we did