Remove and add item in a list
Write a program to remove the item present at index 4 and add it to the 2nd position and at the end of the list.
Given:
Expected Output:
List After removing element at index 4 [34, 54, 67, 89, 43, 94]
List after Adding element at index 2 [34, 54, 11, 67, 89, 43, 94]
List after Adding element at last [34, 54, 11, 67, 89, 43, 94, 11]
Hint:
Use the list methods,
pop()
,insert()
andappend()
Solution:
pop(index)
: Removes and returns the item at the given index from the list.insert(index, item)
: Add the item at the specified position(index) in the listappend(item)
: Add item at the end of the list.