Monday 14 August 2023

Effectively Combining Iterables with Zip function

Using Python’s zip function, we can combine multiple iterables into a single iterable. Here iterable can be a list, tuple etc.,

For example, I have 4 lists to represent employees information and I used zip() to create a list of tuples, where each tuple contains the corresponding elements from the four lists.

ids = [1, 2, 3, 4]
names = ['Krishna', 'Narasimha', 'Ramesh', 'Sailu']
ages = [34, 39, 45, 35]
cities = ['Bangalore', 'Chennai', 'Bangalore', 'Hyderabad']

zipped = zip(ids, names, ages, cities)
print(zipped)

 

This code will print the following output:

<zip object at 0x10b4e6c00>

The zipped variable is a zip object, which is an iterator of tuples, we can iterate over a zipped object using for loop like below.

for id, name, age, city in zipped:
    print(f'id : {id}, name : {name}, age : {age}, city : {city}')

 

Convert zip object to a list of tuples

Using list() function, we can convert the zip object to a list.

list_of_tuples = list(zipped)

Iterate over a zip object using next() function

while True:
    try:
        id, name, age, city = next(zipped)
        print(f'id : {id}, name : {name}, age : {age}, city : {city}')
    except StopIteration:
        print('No more elements to process')
        break

‘next()’ function will return the next tuple in the iterator, and the id, name, age, city variables will be assigned the corresponding elements from the tuple. ‘while’ loop in the code will continue to iterate over the zip object until there are more tuples to be processed. When there are no more tuples to process, next() method raises a StopIteration exception.

 

Can I iterate over a zipped object multiple times?

No, you can’t iterate over a zipped object multiple times, zip() function returns an iterator which can be traversed once.

 

Find the below working application.

 


zip_demo.py

ids = [1, 2, 3, 4]
names = ['Krishna', 'Narasimha', 'Ramesh', 'Sailu']
ages = [34, 39, 45, 35]
cities = ['Bangalore', 'Chennai', 'Bangalore', 'Hyderabad']

zipped = zip(ids, names, ages, cities)
print(zipped)

# iterating over the zipped object using for loop
print('\niterating over the zipped object using for loop')
for id, name, age, city in zipped:
    print(f'id : {id}, name : {name}, age : {age}, city : {city}')

# I can't iterate over a zipped object multiple times, so creating new one
zipped = zip(ids, names, ages, cities)
print('Convert the zipped object to a list')
list_of_tuples = list(zipped)
print(f'\nlist_of_tuples : \n{list_of_tuples}')

# I can't iterate over a zipped object multiple times, so creating new one
print('\nIterate over zipped object using next function')
zipped = zip(ids, names, ages, cities)
while True:
    try:
        id, name, age, city = next(zipped)
        print(f'id : {id}, name : {name}, age : {age}, city : {city}')
    except StopIteration:
        print('No more elements to process')
        break

Output

<zip object at 0x107ee0e80>

iterating over the zipped object using for loop
id : 1, name : Krishna, age : 34, city : Bangalore
id : 2, name : Narasimha, age : 39, city : Chennai
id : 3, name : Ramesh, age : 45, city : Bangalore
id : 4, name : Sailu, age : 35, city : Hyderabad
Convert the zipped object to a list

list_of_tuples : 
[(1, 'Krishna', 34, 'Bangalore'), (2, 'Narasimha', 39, 'Chennai'), (3, 'Ramesh', 45, 'Bangalore'), (4, 'Sailu', 35, 'Hyderabad')]

Iterate over zipped object using next function
id : 1, name : Krishna, age : 34, city : Bangalore
id : 2, name : Narasimha, age : 39, city : Chennai
id : 3, name : Ramesh, age : 45, city : Bangalore
id : 4, name : Sailu, age : 35, city : Hyderabad
No more elements to process

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment