Coverter the type in the array

Question:

how to convert to the form like me in the example below:
ex:

[a, b, c,d ] >> [(a,b) (c,d)]

[a,b,c,d] 
#wish to be like this
[(a,b) (c,d)]

Thanks

Asked By: Quan Tran

||

Answers:

Try this;

l = ['a','b','c','d']
n = 2
lst = []
for i in range(0, len(l), n):
    lst.append(tuple(l[i:i + n]))
#Output
[('a', 'b'), ('c', 'd')]
Answered By: Sachin Kohli
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.