Convert a List of Tuples to a List of Lists

Convert a list of tuples to a list of lists

You can use list comprehension:

>>> list_of_tuples = [(1, 2), (4, 5)]
>>> list_of_lists = [list(elem) for elem in list_of_tuples]

>>> list_of_lists
[[1, 2], [4, 5]]

How to convert a tuple of tuples to a list of lists?

Is this what you want? -

In [17]: a = ((1,2,3),(4,5,6),(7,8,9))

In [18]: b = [list(x) for x in a]

In [19]: b
Out[19]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

This is called list comprehension, and when you do list(x) where x is an iterable (which also includes tuples) it converts it into a list of same elements.

Merge a list of tuples element wise and transform it to a list of lists

You can zip your list elements together, and use itertools to chain/flatten the result within a list comprehension.

>>> import itertools
>>> [list(itertools.chain.from_iterable(i)) for i in zip(*list_results)]
[[{'A'}, {'B'}, {'C'}, {'D'}], [55, 422], [23], [1]]

Python: transform a list of lists of tuples

You want the columns, so you can use zip function :

zip(*main_list)

But since it returns the columns in tuple format if you only want list you can use map to convert them to list :

map(list,zip(*main_list))

Demo:

>>> main_list=[[(1,2),(3,4)],[(5,6),(7,8)]]
>>> zip(*main_list)
[((1, 2), (5, 6)), ((3, 4), (7, 8))]

And with your example :

>>> main_list=[[(np.array([111, 111]), np.array([121, 121])),
... (np.array([112, 112]), np.array([122, 122])),
... (np.array([131, 131]), np.array([141, 141])),
... (np.array([132, 132]), np.array([142, 142]))],
... [(np.array([211, 211]), np.array([221, 221])),
... (np.array([212, 212]), np.array([222, 222])),
... (np.array([231, 231]), np.array([241, 241])),
... (np.array([232, 232]), np.array([242, 242]))]]
>>>
>>> zip(*main_list)
[((array([111, 111]), array([121, 121])), (array([211, 211]), array([221, 221]))),
((array([112, 112]), array([122, 122])), (array([212, 212]), array([222, 222]))),
((array([131, 131]), array([141, 141])), (array([231, 231]), array([241, 241]))),
((array([132, 132]), array([142, 142])), (array([232, 232]), array([242, 242])))]

How to convert nested list of lists into a list of tuples in python 3.3?

Just use a list comprehension:

nested_lst_of_tuples = [tuple(l) for l in nested_lst]

Demo:

>>> nested_lst = [['tom', 'cat'], ['jerry', 'mouse'], ['spark', 'dog']]
>>> [tuple(l) for l in nested_lst]
[('tom', 'cat'), ('jerry', 'mouse'), ('spark', 'dog')]

Convert a list of lists back into a list of tuples

tuple(myList) will convert myList into a tuple, provided that myList is something iterable like a list, a tuple, or a generator.

To convert a lists of lists in a list of tuples, using a list comprehension expression:

last_list = [tuple(x) for x in Long_list]

or, to also perform your string replacement:

last_list = [tuple(y.replace('/', '@') for y in x) for x in Long_list]

From Python's reference:

tuple( [iterable] )

Return a tuple whose items are the same and in the same order as iterable‘s items. iterable may be a sequence, a container that supports iteration, or an iterator object. If iterable is already a tuple, it is returned unchanged. For instance, tuple('abc') returns ('a', 'b', 'c') and tuple([1, 2, 3]) returns (1, 2, 3). If no argument is given, returns a new empty tuple, ().

tuple is an immutable sequence type, as documented in Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange. For other containers see the built in dict, list and [set] classes, and the collections module.



Related Topics



Leave a reply



Submit