B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Change List Items in Python | LiveCodeProgramming
Python Installation First Python Program Control Flow if...else nested if else match-case logical-operators for loops while loops break / continue Functions Defining Functions Function Arguments Function return value Lambda Function Recursion Exception Handling Exception Handling Try Except Else and Finally Raise Exception Custom Exceptions File Handling File in python Read File Write File Append File Delete File Exception Handling

Changing List Items in Python

You can change list items in Python using direct index assignment, slices, or loops. Here are various methods:

Python में list के items को update करने के लिए हम index, slice या loop का उपयोग कर सकते हैं।

1. Change item using index
names = ["Raj", "Simran", "Aman"]
names[1] = "Pooja"
print(names)
Output:
["Raj", "Pooja", "Aman"]
2. Change multiple items using slice
names = ["Raj", "Simran", "Aman"]
names[0:2] = ["Karan", "Arjun"]
print(names)
Output:
["Karan", "Arjun", "Aman"]
3. Replace last item using negative index
names = ["Raj", "Simran", "Aman"]
names[-1] = "Preeti"
print(names)
Output:
["Raj", "Simran", "Preeti"]
4. Change all items using loop
names = ["Raj", "Simran", "Aman"]
for i in range(len(names)):
    names[i] = names[i].upper()
print(names)
Output:
["RAJ", "SIMRAN", "AMAN"]
5. Update by index using user input
names = ["Raj", "Simran", "Aman"]
index = 1
new_name = "Deepika"
names[index] = new_name
print(names)
Output:
["Raj", "Deepika", "Aman"]