CodeGym /Courses /Python SELF EN /Modifying Lists

Modifying Lists

Python SELF EN
Level 7 , Lesson 4
Available

5.1 Adding Elements

In Python, there are several ways to add elements to a list, making lists one of the most versatile data structures in the language. Here are the main methods Python provides for adding elements to a list:

Using the method append()

The append() method adds an element to the end of the list. It's the simplest and most commonly used method for adding a single element:


my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # Will print [1, 2, 3, 4] 
        

Using the method extend()

The extend() method allows you to add multiple elements to a list at once. The method takes an iterable object as an argument (like another list, tuple, or set):


my_list = [1, 2, 3, 4]
my_list.extend([5, 6])
print(my_list)  # Will print [1, 2, 3, 4, 5, 6]
        

Using the method insert()

The insert() method adds an element at a specified position in the list. This method takes two arguments: the index where the element should be placed and the element itself:


my_list = [1, 2, 3, 4, 5, 6]
my_list.insert(0, 0)
print(my_list)  # Will print [0, 1, 2, 3, 4, 5, 6]
    

Adding elements using the addition operator

You can also merge lists using the '+' operator, which results in adding elements from one list to another:


my_list = [0, 1, 2, 3, 4, 5, 6]
my_list = my_list + [7, 8]
print(my_list)  # Will print [0, 1, 2, 3, 4, 5, 6, 7, 8]
        

Features and Warnings

When using append() and extend(), the original list is modified. However, when using '+', a new list is created, so if you have a reference to the old list somewhere, it will remain unchanged.

5.2 Directly Modifying an Element

In Python, modifying elements in a list is simple thanks to the mutability of lists. This allows you to modify lists in place without creating new copies. Here's how you can work with these capabilities:

Directly Modifying Elements

To change an element in a list, simply assign a new value to an index:


my_list = [1, 2, 3, 4]
my_list[2] = 30 
print(my_list)  # Will print [1, 2, 30, 4] 
        

5.3 Assigning a Range of Elements:

In Python, assigning a range of elements in a list is done using slices. Slices not only allow you to retrieve sublists but also to assign them. This makes lists in Python particularly flexible for working with sequences of data.

Using slices for assignment

Slices in Python can be used to modify multiple list elements at once. For example, you can replace part of a list with other values by specifying a slice and assigning it a new list of elements:


numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers[2:5] = [20, 30, 40]
print(numbers)  # Will print [0, 1, 20, 30, 40, 5, 6, 7, 8, 9]
        

The number of elements on the left and right of the equals sign does not have to match.

There can be more elements on the right:


numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers[2:2] = [20, 30, 40]
print(numbers)  # Will print [0, 1, 20, 30, 40, 2, 3, 4, 5, 6, 7, 8, 9]
        

Or fewer:


numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers[2:9] = [20, 30, 40]
print(numbers)  # Will print [0, 20, 30, 40, 9]
        

This approach allows you to write very compact code, but remember that when working with slices, the last element is not included in the range.

2
Task
Python SELF EN, level 7, lesson 4
Locked
Who wants more?
Who wants more?
2
Task
Python SELF EN, level 7, lesson 4
Locked
Eraser
Eraser
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION