-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip() in python
More file actions
149 lines (88 loc) · 3.08 KB
/
zip() in python
File metadata and controls
149 lines (88 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
The zip() function in Python combines multiple iterables (like lists, tuples, etc.) element by element into pairs (or tuples).
It’s handy for processing two or more sequences in parallel.
"""
#Syntax
zip(*iterables)
"""
- *iterables : Any number of iterables (e.g., lists, tuples, strings).
- The result is an iterator of tuples where the first element comes from the first iterable, the second from the second iterable, and so on.
---
#Key Points
1. If the iterables are of unequal length, zip() stops as soon as the shortest iterable is exhausted.
2. It returns an iterator , so it’s lazy (efficient with memory). To see the results directly, you often convert it to a list or iterate over it.
---
#Basic Example
"""
questions = ['What is your name?', 'How old are you?', 'Where do you live?']
answers = ['Alice', '25', 'Paris']
zipped = zip(questions, answers)
# Convert to a list to display the result
print(list(zipped))
#Output :
[('What is your name?', 'Alice'), ('How old are you?', '25'), ('Where do you live?', 'Paris')]
---
# Using zip() in a Loop :-
for question, answer in zip(questions, answers):
print(f"Q: {question}")
print(f"A: {answer}")
"""
Output :
Q: What is your name?
A: Alice
Q: How old are you?
A: 25
Q: Where do you live?
A: Paris
---
# With Unequal Length Iterables:-
"""
questions = ['What is your name?', 'How old are you?']
answers = ['Alice', '25', 'Paris']
zipped = zip(questions, answers)
print(list(zipped))
#Output :
[('What is your name?', 'Alice'), ('How old are you?', '25')]
#Here, zip() stops after the shortest iterable ( questions ) is exhausted.
---
#Combining Multiple Iterables
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
cities = ['Paris', 'London', 'New York']
zipped = zip(names, ages, cities)
for name, age, city in zipped:
print(f"{name} is {age} years old and lives in {city}.")
#Output :
Alice is 25 years old and lives in Paris.
Bob is 30 years old and lives in London.
Charlie is 35 years old and lives in New York.
---
#Unzipping Using zip()
#You can reverse the operation by using * (unpacking operator):
zipped = [('Alice', 25), ('Bob', 30), ('Charlie', 35)]
names, ages = zip(*zipped)
print(names)
print(ages)
#Output :
('Alice', 'Bob', 'Charlie')
(25, 30, 35)
---
#Practical Use Case
#Let’s combine enumerate() and zip() to process items with indices:
questions = ['What is your name?', 'How old are you?', 'Where do you live?']
answers = ['Alice', '25', 'Paris']
for i, (question, answer) in enumerate(zip(questions, answers), start=1):
print(f"Q{i}: {question}")
print(f"A{i}: {answer}")
#Output :
Q1: What is your name?
A1: Alice
Q2: How old are you?
A2: 25
Q3: Where do you live?
A3: Paris
---
#Summary:-
"""
The zip() function is a powerful and concise way to work with multiple iterables in parallel. Its main advantages are readability, efficiency, and the ability to combine and process data from different sources.
"""