0
['1899', 'Horsey', '1909', 'Ford', '1911', 'Overland',]

For example, I want to convert the above list into this form:

['1899', 'Horsey']['1909', 'Ford']['1911', 'Overland']

How can I do that? I'm very new to python.

nglee
  • 1,913
  • 9
  • 32
Sam Wu
  • 1
  • An efficient way https://stackoverflow.com/questions/8991506/iterate-an-iterator-by-chunks-of-n-in-python – Priyesh Kumar May 30 '17 at 14:44
  • 2
    Possible duplicate of [What is the most "pythonic" way to iterate over a list in chunks?](https://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks) – dnapierata May 30 '17 at 14:47

1 Answers1

0

You can use list comprehension in python

Origlist = ['1899', 'Horsey', '1909', 'Ford', '1911', 'Overland',]
sublistsize = 2
sublists = [Origlist [x:x+sublistsize] for x in range(0,len(Origlist ),sublistsize)]
Aafaque Abdullah
  • 361
  • 3
  • 13
  • im running your code and get [['1899', 'Horsey'], ['1909', 'Ford'], ['1911', 'Overland'], ['1958', 'Ford'], ['1961', 'Amphicar'], ['1961', 'Corvair']], but i wanna remove the outside brackets to make like: ['1899', 'Horsey'] ['1909', 'Ford'] ['1958', 'Ford'] how can i achieve that? – Sam Wu May 31 '17 at 07:04
  • So you want these to save in some variable to use later or just print in the form you have mentioned ? – Aafaque Abdullah May 31 '17 at 13:56