Saturday, 2 March 2024

The zip function in Python

 


Example 1: Basic Usage of zip

# Basic usage of zip

names = ["Alice", "Bob", "Charlie"]

ages = [25, 30, 35]

# Combining lists using zip

combined_data = zip(names, ages)

# Displaying the result

for name, age in combined_data:

    print(f"Name: {name}, Age: {age}")

    #clcoding.com

Name: Alice, Age: 25

Name: Bob, Age: 30

Name: Charlie, Age: 35

Example 2: Different Lengths in Input Iterables

# Zip with different lengths in input iterables

names = ["Alice", "Bob", "Charlie"]

ages = [25, 30]

# Using zip with different lengths will stop at the shortest iterable

combined_data = zip(names, ages)

# Displaying the result

for name, age in combined_data:

    print(f"Name: {name}, Age: {age}")

    

#clcoding.com

Name: Alice, Age: 25

Name: Bob, Age: 30

Example 3: Unzipping with zip

# Unzipping with zip

names = ["Alice", "Bob", "Charlie"]

ages = [25, 30, 35]

# Combining lists using zip

combined_data = zip(names, ages)

# Unzipping the result

unzipped_names, unzipped_ages = zip(*combined_data)

# Displaying the unzipped data

print("Unzipped Names:", unzipped_names)

print("Unzipped Ages:", unzipped_ages)

#clcoding.com

Unzipped Names: ('Alice', 'Bob', 'Charlie')

Unzipped Ages: (25, 30, 35)

Example 4: Using zip with Dictionaries

# Using zip with dictionaries

keys = ["name", "age", "city"]

values = ["Alice", 25, "New York"]

# Creating a dictionary using zip

person_dict = dict(zip(keys, values))

# Displaying the dictionary

print(person_dict)

#clcoding.com

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

Example 5: Transposing a Matrix with zip

# Transposing a matrix using zip

matrix = [

    [1, 2, 3],

    [4, 5, 6],

    [7, 8, 9]

]

# Using zip to transpose the matrix

transposed_matrix = list(zip(*matrix))

# Displaying the transposed matrix

for row in transposed_matrix:

    print(row)

#clcoding.com

(1, 4, 7)

(2, 5, 8)

(3, 6, 9)

Example 6: Using zip with enumerate

# Using zip with enumerate

names = ["Alice", "Bob", "Charlie"]

# Combining with enumerate to get both index and value

indexed_names = list(zip(range(len(names)), names))

# Displaying the result

for index, name in indexed_names:

    print(f"Index: {index}, Name: {name}")

#clcoding.com

Index: 0, Name: Alice

Index: 1, Name: Bob

Index: 2, Name: Charlie

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (363) Python Quiz (25) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses