## Grouping lists within lists in Python 3

By : Tal
Source: Stackoverflow.com
Question!

I have a list of lists of strings like so:

``````List1 = [
['John', 'Doe'],
['1','2','3'],
['Henry', 'Doe'],
['4','5','6']
]
``````

That I would like to turn into something like this:

``````List1 = [
[ ['John', 'Doe'], ['1','2','3'] ],
[ ['Henry', 'Doe'], ['4','5','6'] ]
]
``````

But I seem to be having trouble doing so.

By : Tal

``````def pairUp(iterable):
``` By : ninjagecko ```
``` This should do what you want assuming you always want to take pairs of the inner lists together. list1 = [['John', 'Doe'], ['1','2','3'], ['Henry', 'Doe'], ['4','5','6']] output = [list(pair) for pair in zip(list1[::2], list1[1::2])] It uses zip, which gives you tuples, but if you need it exactly as you've shown, in lists, the outer list comprehension does that. By : recursive List1 = [['John', 'Doe'], ['1','2','3'], ['Henry', 'Doe'], ['4','5','6'], ['Bob', 'Opoto'], ['10','11','12']] def pairing(iterable): it = iter(iterable) itn = it.next for x in it : yield (x,itn()) # The generator pairing(iterable) yields tuples: for tu in pairing(List1): print tu # produces: (['John', 'Doe'], ['1', '2', '3']) (['Henry', 'Doe'], ['4', '5', '6']) (['Bob', 'Opoto'], ['8', '9', '10']) # If you really want a yielding of lists: from itertools import imap # In Python 2. In Python 3, map is a generator for li in imap(list,pairing(List1)): print li # or defining pairing() precisely so: def pairing(iterable): it = iter(iterable) itn = it.next for x in it : yield [x,itn()] # produce [['John', 'Doe'], ['1', '2', '3']] [['Henry', 'Doe'], ['4', '5', '6']] [['Bob', 'Opoto'], ['8', '9', '10']] Edit: Defining a generator function isn't required, you can do the pairing of a list on the fly: List1 = [['John', 'Doe'], ['1','2','3'], ['Henry', 'Doe'], ['4','5','6'], ['Bob', 'Opoto'], ['8','9','10']] it = iter(List1) itn = it.next List1 = [ [x,itn()] for x in it] By : eyquem This video can help you solving your question :) By: admin ```
``` Related Questions Py3k memory conservation by returning iterators rather than lists Python - Check if numbers in list are factors of a number python:[[1,2],[3,4],[5,6],[7,8]] transform into [[1],[2,3],[4,5],[6,7],[8]] and vice versa python tuple list sorting (adsbygoogle = window.adsbygoogle || []).push({}); Python Lists and Yielding Getting a list from a config file with ConfigParser in Python Shortest way to do this Python 3 list operation in PHP, JavaScript? Maintaining sequence type in set or list comprehension in python Finding consecutive values within a list What exactly is happening when I copy and then edit this list? Comparing element in a list Replace for loop with list comprehension Extending a python list class to perform additional functions on its contents? Append result of variable to a list How is memory allocated for empty list, tuple, dictionary? How can I create a new list with each element a regular expression of each element of another list in python? numbers in a list, find average of surroundings Removing all elements containing (“,”) from a list python: opening text files as lists, and retrieving data with them ```
``` ```
``` About Us    Contact Us    Legal    feedback    Copyright © 2015 - All Rights Reserved - www.4answered.com 'undefined'=== typeof _trfq || (window._trfq = []);'undefined'=== typeof _trfd && (window._trfd=[]),_trfd.push({'tccl.baseHost':'secureserver.net'}),_trfd.push({'ap':'cpsh'},{'server':'p3plcpnl0831'}) // Monitoring performance to make your website faster. If you want to opt-out, please contact web hosting support.```