-1

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

1
  • What is the generalization here? If you want [(a,b) (c,d)] then wanted = [(a,b) (c,d)] works fine. Please explain the logic of how you get from [a, b, c, d ] to [(a,b) (c,d)]. What should [a, b, c, d, e] be? Why? Are you just looking for chunking to a size?
    – Mark
    Commented Oct 6, 2022 at 3:58

1 Answer 1

0

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')]
1
  • 1
    i found the fix it was to use dlib: dlib.rectangle(a,b,c,d)
    – Quan Tran
    Commented Oct 6, 2022 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.