Write a Python program to print a specified list after removing the 0th, 4th and 5th elements
The program is written in python and it is used to remove certain elements from a list of animals.
- The first line of the program initializes a list of animals named "animal"
- The second line uses a list comprehension to create a new list of animals by iterating over the original list and enumerating each element.
- The "enumerate" function adds a counter to the list of animals, so the list comprehension can reference the index of each element.
- The list comprehension then uses an if statement to check if the index of the element is not equal to 0, 4 or 5. If this is true, it appends the element to the new list.
- Finally, the program prints the new list of animals with the desired elements removed.
Source Code
# 0 1 2 3 4 5 6
animal = ["Cat", "Dog", "Elephant", "Fox", "Tiger", "Lion", "Ponda"]
animal = [x for (i,x) in enumerate(animal) if i not in (0,4,5)]
print(animal)
Output
['Dog', 'Elephant', 'Fox', 'Ponda']