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.

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

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)

Output:

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.

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.

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)

Output:

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.

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

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)

Output:

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.

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.

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)

Output:

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.

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.

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.

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)

Output:

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.

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.

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)

Output:

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.

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.

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

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)

Output:

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.

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.

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)

Output:

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.

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

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)

Output:

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.

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

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)

Output:

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.

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.

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)

Output:

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.

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.

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)

Output:

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.

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.

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)

Output:

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

Section titled “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.

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)

Output:

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.

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

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)

Output:

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.

You can sort a list in descending order by using the sort() method with the argument 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)

Output:

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.

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

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)

Output:

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

Section titled “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.

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)

Output:

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.

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.

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)

Output:

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

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.

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)

Output:

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

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.

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)

Output:

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

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

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)

Output:

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.

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

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)

Output:

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.

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

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)

Output:

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.

You can copy a list using slicing. The slicing syntax is 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)

Output:

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.

You can copy a list using list comprehension. The list comprehension syntax is [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)

Output:

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.

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.

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)

Output:

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.

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

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)

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]

In the example above, we joined two lists.

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

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)

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]

In the example above, we joined two lists.

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

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)

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]

In the example above, we joined two lists.

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.

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)

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]

In the example above, we joined two lists.

You can join two lists by using list comprehension. The list comprehension syntax is [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)

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]

In the example above, we joined two lists.

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.

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)

Output:

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)

Output:

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() 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.

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)

Output:

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.

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.


Exercise 3 – Filter with List Comprehension

Section titled “Exercise 3 – Filter with List Comprehension”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading