+ 1
Please explain what this code is
a=[1, 2, 3] print (âaâ..âcâ).zip(a) #outputs [[âaâ, 1], [âbâ, 2], [âcâ, 3]] I dont understand what the .zip method does. Can someone please explain it?
2 Answers
+ 6
.zip is useful for combining collections in an ordered way. Weâll first call it with two collections, one as the message receiver and one as the messageâs argument:array.zip(other_array) The order is irrelevant for now. .zip will return arrays made by combining the collections piece by piece.
So far, simple:
[1,2,3].zip(['a', 'b', 'c']) #=> [[1, "a"], [2, "b"], [3, "c"]]
ex:-
a=[1,2,3]
print('a'.. 'c') =['a', 'b', 'c']
print('a'.. 'c').zip[1,2,3]=># will be written as below
print('a','b','c').zip[1,2,3]
it print the list element in order 0th element combine with 0th element of other list and so on
so 'a' is combine with 1 'b' is combine with 2 and 'c' is combine with 3 so output came as
[['a', 1],['b',2],['c', 3]]
0
Thank you so much for your reply!