Change Python List Items
Learn how to change items in a Python list with 10 examples covering modification, insertion, replacement, and deletion.
Python list में items को बदलना सीखें। 10 उदाहरणों में modification, insertion, replacement, और deletion शामिल हैं।
Example 1: Modify an Item by Index
fruits = ['apple', 'banana', 'cherry']
fruits[1] = 'orange'
print(fruits)
Change 'banana' to 'orange' using index 1.
index 1 से 'banana' को 'orange' में बदलें।
Output:
Example 2: Modify Multiple Items Using Slice
numbers = [1, 2, 3, 4, 5]
numbers[1:3] = [20, 30]
print(numbers)
Replace elements at index 1 and 2 with 20 and 30.
index 1 और 2 के elements को 20 और 30 से बदलें।
Output:
Example 3: Insert Item at Specific Position
colors = ['red', 'green', 'blue']
colors.insert(2, 'yellow')
print(colors)
Insert 'yellow' at index 2.
index 2 पर 'yellow' insert करें।
Output:
Example 4: Append Item at the End
colors = ['red', 'green']
colors.append('blue')
print(colors)
Add 'blue' to the end of the list.
List के अंत में 'blue' जोड़ें।
Output:
Example 5: Extend List by Another List
list1 = [1, 2]
list2 = [3, 4]
list1.extend(list2)
print(list1)
Add all elements of list2 to list1.
list2 के सभी elements को list1 में जोड़ें।
Output:
Example 6: Remove Item by Value
fruits = ['apple', 'banana', 'cherry']
fruits.remove('banana')
print(fruits)
Remove 'banana' from the list.
'banana' को list से हटाएं।
Output:
Example 7: Remove Item by Index Using pop()
numbers = [10, 20, 30]
numbers.pop(1)
print(numbers)
Remove element at index 1 (value 20).
index 1 का element हटाएं (value 20)।
Output:
Example 8: Clear the Entire List
colors = ['red', 'green', 'blue']
colors.clear()
print(colors)
Remove all items from the list.
List के सभी items हटाएं।
Output:
Example 9: Replace List Completely
fruits = ['apple', 'banana']
fruits[:] = ['mango', 'pineapple']
print(fruits)
Replace entire list content using slice assignment.
slice assignment से पूरी list बदलें।
Output:
Example 10: Change Nested List Item
matrix = [[1, 2], [3, 4]]
matrix[1][0] = 30
print(matrix)
Change nested list element at row 1, col 0 to 30.
nested list के row 1, col 0 के element को 30 में बदलें।
Output: