-1

By simply technique i can think of, I am converting a list to 2d list, However is it possible to achievie this with higher performance either by itertools or collections?

n = 2
lst = ['a', 'b', 'c', 2, 'e']
for i in  range(0, len(lst), n):
    print lst[i:i+n]

Expected output:

[['a', 'b'], ['c', 2], ['e']]
Steinar Lima
  • 7,644
  • 2
  • 39
  • 40
Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

1 Answers1

0
>>> thing = [lst[i:i+n] for i in range(0,len(lst),n)]
>>> thing
[['a', 'b'], ['c', 2], ['e']]

Using a list comprehension

kylieCatt
  • 10,672
  • 5
  • 43
  • 51