Use indexes to access same-indexes items:
def zippy(x, y):
zipper = []
for i in range(len(x)):
zipper.append((x[i], y[i]))
return zipper
using list comprehension:
def zippy(x, y):
return [(x[i], y[i]) for i in range(len(x))]
>>> fr = [6, 5, 4] >>> tr = [7, 8, 9] >>> zippy(fr, tr)[(6, 7), (5, 8), (4, 9)]
Please consider that if you have two lists of different length your zip should stop when the shortest sequence ends. Here is an example of how to do it:
def zippy(x, y):
iter1 = iter(x)
iter2 = iter(y)
while True:
try:
yield next(iter1), next(iter2)
except StopIteration:
return
or
def zippy(x, y):
iter1 = iter(x)
iter2 = iter(y)
for _ in range(min(len(x), len(y))):
yield next(iter1), next(iter2)
Also notice that standard zip function returns (generator) and not a list of data! That means if you need to get data from your result you have to convert your zip result to list:
result = zippy(fr, tr) print(list(result))
I would suggest using range()
to cycle through each index of the array. Then, put them in a tuple and append them to the array.
def zippy(x, y):
zipper = []
for i in range(len(x)):
zipper.append((x[i], y[i]))
return zipper
You need to iterate both arrays at the same time to get the same indexes from both arrays
def zippy(x, y):
zipper = []
for i, g in zip(x, y):
t = (i, g)
zipper.append(t)
print(zipper)
Output
[(6, 7), (5, 8), (4, 9)]
# A zip() is a kind of map() !
def zippy(a, b):
return map(lambda(i, a): (a, b[i]), enumerate(a))
Also notice that standard zip function anycodings_python returns (generator) and not a list of anycodings_python data! That means if you need to get data anycodings_python from your result you have to convert anycodings_python your zip result to list:,as a personal exercise I am trying to create anycodings_list my own little zip() function that takes two anycodings_list lists and puts the items into a list of anycodings_list tuples. In other words if these are my anycodings_list list:,Please consider that if you have two anycodings_python lists of different length your zip anycodings_python should stop when the shortest sequence anycodings_python ends. Here is an example of how to do anycodings_python it:,Count the number of files in, and below a directory in Linux C recursively
as a personal exercise I am trying to create anycodings_list my own little zip() function that takes two anycodings_list lists and puts the items into a list of anycodings_list tuples. In other words if these are my anycodings_list list:
fr = [6, 5, 4] tr = [7, 8, 9]
I would expect this:
[(6, 7), (5, 8), (4, 9)]
Here is my code:
def zippy(x, y):
zipper = []
for i in x:
for g in y:
t = (i, g)
zipper.append(t)
Use indexes to access same-indexes anycodings_python items:
def zippy(x, y):
zipper = []
for i in range(len(x)):
zipper.append((x[i], y[i]))
return zipper
using list comprehension:
def zippy(x, y):
return [(x[i], y[i]) for i in range(len(x))]
>>> fr = [6, 5, 4] >>> tr = [7, 8, 9] >>> zippy(fr, tr)[(6, 7), (5, 8), (4, 9)]
Please consider that if you have two anycodings_python lists of different length your zip anycodings_python should stop when the shortest sequence anycodings_python ends. Here is an example of how to do anycodings_python it:
def zippy(x, y):
iter1 = iter(x)
iter2 = iter(y)
while True:
try:
yield next(iter1), next(iter2)
except StopIteration:
return
or
def zippy(x, y):
iter1 = iter(x)
iter2 = iter(y)
for _ in range(min(len(x), len(y))):
yield next(iter1), next(iter2)
Also notice that standard zip function anycodings_python returns (generator) and not a list of anycodings_python data! That means if you need to get data anycodings_python from your result you have to convert anycodings_python your zip result to list:
result = zippy(fr, tr) print(list(result))
I would suggest using range() to cycle anycodings_python through each index of the array. Then, anycodings_python put them in a tuple and append them to anycodings_python the array.
def zippy(x, y):
zipper = []
for i in range(len(x)):
zipper.append((x[i], y[i]))
return zipper
You need to iterate both arrays at the anycodings_python same time to get the same indexes from anycodings_python both arrays
def zippy(x, y):
zipper = []
for i, g in zip(x, y):
t = (i, g)
zipper.append(t)
print(zipper)
Output
[(6, 7), (5, 8), (4, 9)]
# A zip() is a kind of map() !
def zippy(a, b):
return map(lambda(i, a): (a, b[i]), enumerate(a))
Last Updated : 30 Sep, 2021,GATE CS 2021 Syllabus
Output:
{
('Shambhavi', 3), ('Nikhil', 1), ('Astha', 2), ('Manjeet', 4)
}
Output:
The zipped result is: [('Manjeet', 4, 40), ('Nikhil', 1, 50),
('Shambhavi', 3, 60), ('Astha', 2, 70)
]
The unzipped result:
The name list is: ('Manjeet', 'Nikhil', 'Shambhavi', 'Astha')
The roll_no list is: (4, 1, 3, 2)
The marks list is: (40, 50, 60, 70)
The zip() function takes a number of iterables and aggregates them to a single one by combining the i-th values of each iterable into a tuple. For example, zip together lists [1, 2, 3] and [4, 5, 6] to [(1,4), (2,5), (3,6)]. You can unzip this list of tuples by calling zip(*list) using the unpacking (asterisk) operator *. ,The rest of the article is about answering any question you may have regarding the zip() function.,Actually not. The result of the zip() function is an iterator. An iterator in Python is an object that contains a fixed number of elements and allows you to access each element in an ordered fashion (the next(iterator) function for an iterator). This is more efficient and more general-purpose — compared to creating a list and returning the list as a result.,An iterable is an object that contains elements over which you can iterate. Examples are lists, sets, or tuples.
Say, you have two lists:
[1, 2, 3]
[4, 5, 6]
Now you zip them together and get the new list:
[(1, 4), (2, 5), (3, 6)]
You can unzip them by using the following trick: If you remove the outer bracket of the result (e.g., via the asterisk operator), you get the following three tuples:
(1, 4) (2, 5) (3, 6)
This is the idea in the following code snippet:
lst_1 = [1, 2, 3] lst_2 = [4, 5, 6] # Zip two lists together zipped = list(zip(lst_1, lst_2)) print(zipped) #[(1, 4), (2, 5), (3, 6)] # Unzip to lists again lst_1_new, lst_2_new = zip( * zipped) print(list(lst_1_new)) print(list(lst_2_new))
Just do it. Python simply ignores the remaining elements of the longer list. Here is an example:
print(list(zip([1, 2, 3], [1, 2])))
#[(1, 1), (2, 2)]