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

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')]


You can use map():

>>> list(map(tuple, [['tom', 'cat'], ['jerry', 'mouse'], ['spark', 'dog']]))[('tom', 'cat'), ('jerry', 'mouse'), ('spark', 'dog')]

This is equivalent to a list comprehension, except that map returns a generator instead of a list.