Q:

Write a python program to remove the item present at index 4 and add it to the 2nd position and at the end of the list.

belongs to collection: Python Data Structure Exercises

-2

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:

list1 = [54, 44, 27, 79, 91, 41]

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]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use the list methods, pop()insert() and append()

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 list
  • append(item): Add item at the end of the list.
sample_list = [34, 54, 67, 89, 11, 43, 94]

print("Original list ", sample_list)
element = sample_list.pop(4)
print("List After removing element at index 4 ", sample_list)

sample_list.insert(2, element)
print("List after Adding element at index 2 ", sample_list)

sample_list.append(element)
print("List after Adding element at last ", sample_list)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Slice list into 3 equal chunks and reverse each ch... >>
<< Create a list by picking an odd-index items from t...