Change value of element in list python | Laptrinhcanban.com

HOME › >>

Change value of element in list python

Posted at: https://laptrinhcanban.com/en

Instructions on how to change value of element in list python. You will learn how to change element value in a list python using various methods after this lesson.

After initializing a python List, you can change the value of an element in the list, in a variety of ways.

Change the value of a specific element in list python

When we want to change the value of a specific element in the python list, we need to specify that element’s index and then replace the new value in, with the following syntax:

list [index] = value

Try replacing the value of the second element in the list (whose index equals 1, since the index starts counting from 0) in the following example:

list_fruit = [ "Apple" , "Orange" , "Strawberry" ]
print(list_fruit)
#--> [ "Apple" , "Orange" , "Strawberry" ]

# thay đổi giá trị của phần tử thứ hai trong list
list_fruit[1] = "Grape"
print(list_fruit)
["Apple", "Grape", "Strawberry"]

As you can see, the element Orange with the index 1 specified with list_fruit[1] has been replaced with Grape after performing the value change command.

List python, which is a form of data in which the elements are ordered and can change value after being declared.
Therefore, after we change the value of an element in the list, essentially only the value of that element changes, and the properties of that list (like list name, data type, id of type data etc.) does not change.
So the way we use that list won’t change either. For example with the above results list:

list_fruit = [ "Apple" , "Orange" , "Strawberry" ]

print('data type id =' + str(id(list_fruit)))
#--> data type id =139986404189376

list_fruit[1] = "Grape"


print('data type id =' + str(id(list_fruit)))
#--> data type id =139986404189376

We have changed the value of an element in the list, and the property of the list (the data type id ) has not changed.

Change the value of multiple elements at once by slicing the list

We can change the value of multiple elements at the same time by using the slice function in Python , with the following syntax:

list [start index : end index] = list, string etc

By specifying a start and end index, we will determine the range of elements that need to be valued. Then, by specifying the values ​​that will replace the above range, we proceed to change the value by series.

The method of specifying the index is the same as when we slice the list, the scope begins with the element’s index, and the scope ends with the end element’s index +1.
The values ​​to replace the range above are a range of other elements, with the data type being list, or character, tuple and so on.
Note, if you specify the alternate sequence as a string, for example ABC, the sequence will be treated as a character list with letter-by-letter elements in the sequence, such as ["A", "B", "C"].

In the following example, we will replace a series of elements from second to fourth (i.e. starting index = 1, ending index = 4) in the following list:

mylist = ["A", "B", "C", "D", "E", "F"]
print(mylist)
#--> ["A", "B", "C", "D", "E", "F"]

# Change a series of elements from second to fourth position in list
mylist[1:4] = ["1", "2", "3"]
print(mylist)
#--> ["A", "1", "2", "3", "E", "F"]

Again, the number of elements in the substitution sequence is not necessarily the same as the number of elements in the range we are currently replacing.
More is fine, but less is okay. For example below, we will take a range with fewer elements and replace the elements in the scope of the original list:

mylist = ["A", "B", "C", "D", "E", "F"]
print(mylist)
#--> ["A", "B", "C", "D", "E", "F"]

# Replace a range of elements with a range with fewer elements
mylist[1:4] = ["1", "2"]
print(mylist)
#--> ["A", "1", "2", E", "F"]

Also, in case you want to jump when changing the value of an element within the specified range, for example, remove the first element, then change the second element, and then ignore the third element … , use the jump method when slicing the list as the following example:

mylist = ["A", "B", "C", "D", "E", "F"]
print(mylist)
#--> ["A", "B", "C", "D", "E", "F"]

# jumps an element and changes the value of other elements from the 2nd to the 6th in the list
mylist[1:7:2] = ["1", "2", "3"]
print(mylist)
#--> ["A", "1", "C", "2", "E", "3"]

Summarize

Above, Kiyoshi showed you how tochange the value of an element in list python . To better understand the lesson content, you should practice with the following examples.

And let’s learn more about Python in the next lessons.

colorlist = ["Blue", "Red", "Green", "White"]
print(colorlist)

colorlist[2] ="Orange"
print(colorlist)

colorlist[1:3] = ["Yellow", "Pink", "Black"]
print(colorlist)


colorlist[1:5:2] = ["Gold", "Silver"]
print(colorlist)

URL Link

https://laptrinhcanban.com/en/python/nhap-mon-lap-trinh-python/list-trong-python/thay-doi-gia-tri-cua-phan-tu-trong-list-python/

Thank you for reading and please Like & Share for other friends to learn.
">

HOME  › >>

  • Recent Posts
Profile
きよしです!笑

Author: Kiyoshi (Chis Thanh)

Kiyoshi was a former international student in Japan. After graduating from Toyama University in 2017, Kiyoshi is currently working at BrSE in Tokyo, Japan. Kiyoshi là một cựu du học sinh tại Nhật Bản. Sau khi tốt nghiệp đại học Toyama năm 2017, Kiyoshi hiện đang làm BrSE tại Tokyo, Nhật Bản.