-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathzip_example.py
More file actions
44 lines (37 loc) · 858 Bytes
/
zip_example.py
File metadata and controls
44 lines (37 loc) · 858 Bytes
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
# ZIP
lst1 = [1, 2, 3, 4, 5, 6]
lst2 = [6, 5, 4, 3, 2, 1]
# build lst3: lst3[i] = lst1[i] + lst2[i]
# Old way
lst3 = []
for i in range(len(lst1)):
lst3.append(lst1[i] + lst2[i])
print(lst3)
# Using ZIP
lst3 = []
for i, j in zip(lst1, lst2):
lst3.append(i + j)
print(lst3)
# Not the same length
lst1 = [1, 2, 3, 4, 5, 6]
lst2 = [6, 5, 4, 3]
lst3 = []
for i, j in zip(lst1, lst2):
print(i, j)
lst3.append(i + j)
print(lst3)
# Multiple lists
lst1 = [1, 2, 3, 4, 5, 6]
lst2 = [6, 5, 4, 3, 2, 1]
lst3 = [3, 5, 3, 5, 3]
lst4 = []
for i, j, k in zip(lst1, lst2, lst3):
print(i, j, k)
lst4.append(i + j + k)
print(lst4)
# Dict example
dict1 = {"Name": "Saeed", "Family": "Isa", "Youtube": "Yes"}
dict2 = {"Name": "Salam", "Family": "Isa"}
for i, j in zip(dict1.items(), dict2.items()):
print(i, '----', j)
# Subscribe and share :)