DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 26 of 77
←PreviousPrevNextNext→

Change List Items

Direct Modification

Lists are mutable, meaning their elements can be changed after creation. You can modify values by assigning to an index, replacing slices, or using in-place methods like append() or extend().

Example
colors = ['red', 'green', 'blue']

# Single index assignment
colors[1] = 'yellow'

# Slice assignment (replaces part of the list)
colors[0:2] = ['black', 'white']

# Multiple indices with stride
colors[::2] = ['purple', 'orange']
print(colors)  # ['purple', 'white', 'orange']
Output
['purple', 'white', 'orange']

Slice Replacement Rules

When assigning to a slice, the left side defines which elements are replaced, while the right side must be an iterable providing new values. The number of elements on each side does not have to match.

Example
numbers = [1, 2, 3, 4, 5]

# Replace 2 elements with 3 elements
numbers[1:3] = [20, 30, 40]  # [1, 20, 30, 40, 4, 5]

# Replace 3 elements with 1 element
numbers[2:5] = [300]  # [1, 20, 300, 5]

# Clear the list by replacing all elements
numbers[:] = []
Output
# Results shown in comments

Memory Considerations

Different list modification operations can have varying memory costs. Simple index assignment usually does not reallocate memory, while inserting or significantly extending may require moving or reallocating storage.

OperationMemory ImpactExample
Index assignmentNo reallocationlst[0] = x
Slice assignmentMay reallocatelst[1:3] = [...]
AppendOccasional reallocation (overallocation strategy)lst.append(x)
ExtendOccasional reallocation (overallocation strategy)lst.extend(iterable)
InsertShifts elements, may reallocatelst.insert(0, x)
Test your knowledge: Change List Items
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 26 of 77
←PreviousPrevNextNext→