iterator object by using zip

We can input a set of iterables and zip function will return an object.
name=['Ravi','Raju','Alex']
id=[1,2,3]
MATH=[30,40,50]
my_data=zip(name,id,MATH) # zip object 
print(list(my_data))
Output
[('Ravi', 1, 30), ('Raju', 2, 40), ('Alex', 3, 50)]
We used list in above code.
We can use one set
print(set(my_data))
Output
{('Alex', 3, 50), ('Ravi', 1, 30), ('Raju', 2, 40)}
Using tuple
print(tuple(my_data))
Output
(('Ravi', 1, 30), ('Raju', 2, 40), ('Alex', 3, 50))

zip with different number of elements

We can create up to the shortest element available. Here we have used 2 number of IDs in place of 3
name=['Ravi','Raju','Alex']
id=[1,2] 
MATH=[30,40,50]
my_data=zip(name,id,MATH)
print(list(my_data))
Output
[('Ravi', 1, 30), ('Raju', 2, 40)]
* operator can be used to unzip
name=['Ravi','Raju','Alex']
id=[1,2,3] 
MATH=[30,40,50]
my_data=zip(name,id,MATH)
name,id,mark=zip(*my_data)
print(name)
print(id)
print(mark)
Output
('Ravi', 'Raju', 'Alex')
(1, 2, 3)
(30, 40, 50)

Example 1: Zipping Three or More Iterables

How to zip three or more iterables and handle uneven lengths?

names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
cities = ["New York", "Los Angeles", "Chicago"]

# Zipping three lists together
zipped_data = zip(names, ages, cities)

# Display the zipped output
for item in zipped_data:
    print(item)

Output:

('Alice', 25, 'New York')
('Bob', 30, 'Los Angeles')
('Charlie', 35, 'Chicago')

Example 2: Unzipping to Multiple Lists

We can include an example that demonstrates unzipping back into individual lists, especially useful for reorganizing data.

zipped_data = [('Alice', 25, 'New York'), ('Bob', 30, 'Los Angeles'), ('Charlie', 35, 'Chicago')]

# Unzipping
names, ages, cities = zip(*zipped_data)

print("Names:", names)
print("Ages:", ages)
print("Cities:", cities)

Output:


Names: ('Alice', 'Bob', 'Charlie')
Ages: (25, 30, 35)
Cities: ('New York', 'Los Angeles', 'Chicago')

Example 3: Using Zip with Dictionaries

Demonstrating how to use zip() with dictionaries can expand its applicability in real-world tasks.

keys = ["name", "age", "city"]
values = ["Alice", 25, "New York"]

# Creating a dictionary using zip
person = dict(zip(keys, values))

print(person)

Output:

{'name': 'Alice', 'age': 25, 'city': 'New York'}

Example 4: Handling Uneven Length Iterables with zip_longest

When working with iterables of unequal lengths, the itertools.zip_longest function provides a way to fill missing values with a default.

from itertools import zip_longest

names = ["Alice", "Bob"]
ages = [25, 30, 35]

# Using zip_longest
zipped_data = zip_longest(names, ages, fillvalue="N/A")

# Display the zipped output
for item in zipped_data:
    print(item)

Output:

('Alice', 25)
('Bob', 30)
('N/A', 35)

Use Cases of zip() and unzip()

  • Data Grouping: Combine related data into tuples for easier manipulation.
  • Parallel Iteration: Iterate over multiple iterables simultaneously.
  • Data Rearrangement: Unzip data for separate processing of grouped information.
  • Dictionary Construction: Quickly create dictionaries from two related lists.
  • Handling Missing Data: Use zip_longest to manage uneven lists.

Advanced Example: Parallel Iteration and Conditional Filtering

We can demonstrate how to use zip() for parallel iteration with conditional filtering:

names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
cities = ["New York", "Los Angeles", "Chicago"]

# Parallel iteration with filtering
filtered_data = [(name, age, city) for name, age, city in zip(names, ages, cities) if age > 28]

print("Filtered Data:", filtered_data)

Output:

Filtered Data: [('Bob', 30, 'Los Angeles'), ('Charlie', 35, 'Chicago')]


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer