Iteration over Sequences and Collections (With Examples)

iteration-over-collection-and-sequences-in-python-with-examples

In the previous tutorial, we learn about Truthiness and Falseness in Python (With Examples). So, now we are going to discuss iteration over sequences and collections in python.

Iteration is the process of accessing each element of a sequence or collection one by one.

It is a fundamental concept in programming, and it is essential for manipulating data in sequences and collections like lists, tuples, and dictionaries.

In this article, we will explain how iteration works and provide examples to help you understand how to iterate over sequences and collections.

Iteration Over Lists:

Lists are sequences of elements that can be of different data types. To iterate over a list, you can use a for loop.

Code:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

Output:

apple
banana
cherry

In this code, we have a list of fruits, and we are iterating over each element of the list using a for loop.

The variable fruit takes the value of each element of the list one by one, and we print it on the console.

Iteration Over Tuples:

Tuples are similar to lists, but they are immutable, meaning we cannot change any element in the tuple after creation.

To iterate over a tuple, you can use the same for loop as for lists.

Code:

numbers = (1, 2, 3)
for number in numbers:
    print(number)

Output:

1
2
3

In the above code, we have a tuple of numbers, and we are iterating over each element of the tuple using a for loop.

The variable number takes the value of each element of the tuple one by one, and we print it on the console.

Iteration Over Dictionaries:

Dictionaries are collections of key-value pairs. To iterate over a dictionary, you can use a for loop to iterate over its keys or values.

Code:

person = {"name": "John", "age": 30, "city": "New York"}
for key in person:
    print(key, person[key])

Output:

name John
age 30
city New York

In the code, we have a dictionary representing a person, and we are iterating over its keys using a for loop.

The variable key takes the value of each key of the dictionary one by one, and we use it to access the corresponding value using person[key].

You can also iterate over the values of a dictionary directly.

Code:

person = {"name": "John", "age": 30, "city": "New York"}
for value in person.values():
    print(value)

Output:

John
30
New York

In the above example, we have a dictionary representing a person, and we are iterating over its values using a for loop.

The variable value takes the value of each value of the dictionary one by one, and we print it on the console.

Related Topics:

Here are some related topics you may find helpful:

List Comprehensions in Python:

List comprehensions are a concise way to create lists in Python. They are a combination of a for loop and a conditional statement. Here’s an example.

Code:

numbers = [1, 2, 3, 4, 5]
squares = [num ** 2 for num in numbers if num % 2 == 0]
print(squares)

Output:

[4, 16]

Here we have a list of numbers and use list comprehension to create a new list of squares of only even numbers.

Zip Function in Python:

The zip function is a built-in function in Python. It takes two or more sequences as arguments and returns a list of tuples. Here the i-th tuple contains the i-th element from each input sequence. Here’s an example.

Code:

fruits = ["apple", "banana", "cherry"]
prices = [1.2, 2.3, 3.4]
for fruit, price in zip(fruits, prices):
    print(fruit, price)

Output:

apple 1.2
banana 2.3
cherry 3.4

In the above code, we have two lists, fruits and prices. We use the zip function to iterate over them simultaneously.

The variables fruit and price take the values of each corresponding element of the lists one by one, and we print them on the console.

Enumerate Function in Python:

The enumerate function is a built-in function in Python that adds a counter to an iterable and returns it as an enumerate object. Here’s an example.

Code:

fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
    print(index, fruit)

Output:

0 apple
1 banana
2 cherry

So here we have a list of fruits, and we use the enumerate function to iterate over it and get both the index and the value of each element.

The variables index and fruit take the values of each corresponding element of the list one by one, and we print them on the console.

In Python, an iteration is a powerful tool for manipulating data in sequences and collections like lists, tuples, and dictionaries.

With a for loop and the right syntax, you can easily process each element of a sequence or collection one by one.

Additionally, the zip and enumerate functions provide additional functionality to iterate over multiple sequences or to get both the index and the value of each element in a sequence.

So, in the next tutorial, we are going to learn about Loop Control Statements. So, make sure to subscribe to us to learn python in an easy way.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x