Professional Documents
Culture Documents
• List
• Sets
• Maps
• Dictionary
• Queue
• Stack
• LinkedList
3.) How can we iterate over collections?
Answer: The iterators are used to provide users with a uniform way of accessing
collections sequentially. An iteration takes elements from a collection one after another,
from one element to the last one. We always need to traverse through the elements of
these collections to fetch the data or make any modifications to that data.
We cannot directly add or remove elements while iterating through the collection that
includes them.
Ex: Program to iterate a collection
collection = [“apple”, “mango”, “cherry”, “banana”]
for i in range(len(collection)):
print(collection[i])
4.) What are the different types of modifications that we can perform on
collections?
Answer: The modifications that we can do on collections.
• Adding Elements During Iteration
To add elements while iterating a list, set or map, keep the new elements in a temporary
list, set, or map and add them to the original after you finish iterating the collection.
• Removing Elements During Iteration
To remove elements from a list, you can create a new list, and then insert the elements
you wish to keep. Or, add the elements you wish to remove to a different list and remove
them after you finish iterating the collection.