Python Central HubSign in

Python Programming / Python List

List Operations

Learn about changing list items in Python. How to insert, append, and remove items from a list. How to change a list item at a specific index. How to change multiple list items at once. How to change a list item to a different type.

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.

Python
list[index] = new_value

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

Python
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
numbers[0] = 10
print(numbers)

Output:

cmd
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:3.

Python
list[start:end] = new_list

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

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

Output:

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

Python
list[index] = new_value

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

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

Output:

cmd
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() method. The insert() method takes two arguments: the index where you want to insert the new item and the new item.

Python
list.insert(index, new_item)

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

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

Output:

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

sketch append is cheap; insert(0) is quadratic p5.js
Both loops build the same n items. append puts each one at the end, which is amortised constant time. insert(0) puts it at the front, which means shifting every element already there -- so the total work grows with the square of n. Measured build times: at 1,000 items insert(0) is about six times slower, at 10,000 about fifty, and at 50,000 about two hundred and fifty. The gap is not a constant factor; it widens forever.

Append List Item

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

Python
list.append(new_item)

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

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

Output:

cmd
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() method. The insert() method takes two arguments: the index where you want to insert the new item and the new item.

Python
list.insert(index, new_item)

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

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

Output:

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

sketch Why append is cheap: the list over-allocates p5.js
A Python list does not grow by one slot per append -- it asks for extra room, so most appends cost nothing but a store. The staircase is the real capacity from sys.getsizeof; the rule that produces it is new = n + (n >> 3) + 6, rounded down to a multiple of 4. That was checked against the interpreter on all fourteen boundaries shown and matches exactly. The spare capacity is why append is amortised O(1) and why a list is usually a little bigger than it looks.

Remove List Item

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

Python
list.remove(item)

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

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

Output:

cmd
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() method. The pop() method takes one argument: the index of the item you want to remove.

Python
list.pop(index)

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

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

Output:

cmd
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() method. The clear() method takes no arguments.

Python
list.clear()

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

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

Output:

cmd
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 del keyword. The del keyword takes one argument: the item you want to remove.

Python
del list[index]

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

Python
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
del numbers[3]
print(numbers)

Output:

cmd
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 del keyword with a slice. The del keyword takes one argument: the slice you want to remove.

Python
del list[start:end]

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

Python
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
del numbers[1:3]
print(numbers)

Output:

cmd
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() function. The enumerate() function takes one argument: the list you want to loop over. The enumerate() function returns a list of tuples. Each tuple contains the index and the item at that index.

Python
for index, item in enumerate(list):
    # do something with index and item

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

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

Output:

cmd
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() function. The enumerate() function takes one argument: the list you want to loop over. The enumerate() function returns a list of tuples. Each tuple contains the index and the item at that index.

Python
[expression for index, item in enumerate(list)]

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

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

Output:

cmd
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() function. The enumerate() function takes one argument: the list you want to loop over. The enumerate() function returns a list of tuples. Each tuple contains the index and the item at that index.

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

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

Output:

cmd
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() method. The sort() method takes no arguments.

Python
list.sort()

The following example shows how to sort a list.

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

Output:

cmd
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() method with the argument reverse=True.

Python
list.sort(reverse=True)

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

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

Output:

cmd
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() function. The sorted() function takes one argument: the list you want to sort.

Python
sorted(list)

The following example shows how to sort a list.

Python
numbers = [5, 2, 4, 1, 3]
print(numbers)
 
numbers = sorted(numbers)
print(numbers)

Output:

cmd
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() function with the argument reverse=True.

Python
sorted(list, reverse=True)

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

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

Output:

cmd
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() method with the argument key. The sort() method takes one argument: the key function.

Python
list.sort(key=key_function)

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

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

Output:

cmd
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 True if the item is even and False 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() method with the argument key and reverse=True. The sort() method takes two arguments: the key function and the reverse flag.

Python
list.sort(key=key_function, reverse=True)

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

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

Output:

cmd
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 True if the item is even and False if the item is odd.

Sort List with Key using Sorted

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

Python
sorted(list, key=key_function)

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

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

Output:

cmd
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 True if the item is even and False 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.

Python
new_list = list

The following example shows how to copy a list.

Python
numbers = [1, 2, 3, 4, 5]
print(numbers)
 
new_numbers = numbers
print(new_numbers)

Output:

cmd
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() method. The copy() method takes no arguments.

Python
new_list = list.copy()

The following example shows how to copy a list.

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

Output:

cmd
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() function. The list() function takes one argument: the list you want to copy.

Python
new_list = list(list)

The following example shows how to copy a list.

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

Output:

cmd
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[:].

Python
new_list = list[:]

The following example shows how to copy a list.

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

Output:

cmd
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].

Python
new_list = [item for item in list]

The following example shows how to copy a list.

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

Output:

cmd
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() function from the copy module. The deepcopy() function takes one argument: the list you want to copy.

Python
new_list = deepcopy(list)

The following example shows how to copy a list.

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

Output:

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

Python
new_list = list1 + list2

The following example shows how to join two lists.

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

Output:

cmd
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() method. The extend() method takes one argument: the list you want to join.

Python
list1.extend(list2)

The following example shows how to join two lists.

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

Output:

cmd
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() method. The append() method takes one argument: the list you want to join.

Python
list1.append(list2)

The following example shows how to join two lists.

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

Output:

cmd
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() method. The insert() method takes two arguments: the index where you want to insert the new list and the new list.

Python
list1.insert(index, list2)

The following example shows how to join two lists.

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

Output:

cmd
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].

Python
new_list = [item for item in list1 + list2]

The following example shows how to join two lists.

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

cmd
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() method. The join() method takes one argument: the list you want to convert to a string.

Python
string = ''.join(list)

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

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

Output:

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

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

Output:

cmd
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() function. The 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.

Python
string = ''.join(map(function, list))

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

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

Output:

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


Try it: List Operations Exercises

Exercise 1 – Concatenate Lists

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – List Comprehension

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Filter with List Comprehension

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did