Python Tutorial List - Modification Element (2 Methods)

Python Exercise Lists

Python provides two methods to modify list elements. You can modify a single element at a time, or you can modify a group (multiple) elements at a time.

Python Modification Element - Single Element

Modifying a single element is very simple, just assign a value to the element directly. Consider the following example:

name = ["Jason", "Alice", "Mark", "Iven", "Peggy"]
name [2] = "Nicole"  #Use positive index
name [-3] = "Susan"  #Use negative index
print (name)

The output is:

['Jason', 'Alice', 'Susan', 'Iven', 'Peggy']

After using the index to get the list element, the value of the element is changed by = assignment.

Python Modification Element - Group Element

Python supports assigning values to a set of elements using a slice syntax. When performing this operation, if you do not specify a step size (step parameter), Python does not require that the number of newly assigned elements is the same as the number of original elements; this means that this operation can add elements to the list or Remove elements from the list.

The following code shows how to modify the value of a group of elements:

name = ["Jason", "Alice", "Mark", "Iven", "Peggy"]
#Modify the value of the 1st to 3rd elements (excluding the 3rd element)
name [1: 3] = ["Nicole", "Kevin", "Peter"]
print (name)

The output is:

['Jason', 'Nicole', 'Kevin', 'Peter', 'Iven', 'Peggy']

Assigning an empty slice is equivalent to inserting a new set of elements:

name = ["Jason", "Alice", "Mark", "Iven", "Peggy"]
#Insert elements in 4 positions
name [4: 4] = ["Nicole", "Kevin", "Peter"]
print (name)

The output is:

['Jason', 'Alice', 'Mark', 'Iven', 'Nicole', 'Kevin', 'Peter', 'Peggy']

Python does not support single values when using slice syntax assignment. For example, the following is wrong:

name = ["Jason", "Alice", "Mark", "Iven", "Peggy"]
#Insert elements in 4 positions
name [4:4] = 33
print (name)

The output is:

TypeError         Traceback (most recent call last)
[ipython-input-88-d4846c25935b] in [module]
1 name = ["Jason", "Alice", "Mark", "Iven", "Peggy"]
2 #Insert elements in 4 positions
----> 3 name [4:4] = 33
4 print (name)
TypeError: can only assign an iterable

But if you use string assignment, Python will automatically convert the string into a sequence, where each character is an element, please see the following code:

s = list("Python")
s[2:4] = "abc"

The output is:

['P', 'y', 'a', 'b', 'c', 'o', 'n']

You can also specify the step size (step parameter) when using the slice syntax, but this time requires that the number of new elements assigned is the same as the number of original elements.

nums = [21, 35, 79, 8, 116, 90, 3]
#Step length is 2, assign values to the 1, 3, and 5 elements
nums [1: 6: 2] = [0.23, -19, 70.8]
print (nums)

The output is:

[21, 0.23, 79, -19, 116, 70.8, 3]


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises