+ 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?

2nd Jan 2018, 2:22 AM
Arfaan
Arfaan - avatar
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]]
2nd Jan 2018, 4:07 AM
GAWEN STEASY
GAWEN STEASY - avatar
0
Thank you so much for your reply!
2nd Jan 2018, 11:41 AM
Arfaan
Arfaan - avatar