Convert to list of lists using Python

Hello coders, in this tutorial we will see how to convert a list to a list of lists and a list of dictionaries to a list of lists in Python.

1. Converting a list to a list of lists:

The given list can be of any data type and using various approaches we can convert list to list of lists.

Approach 1: Using .split()
The following code split() is used to create list of lists. split() helps extracting the elements from a list of strings. Integers don’t have split() attribute so we can’t use split() for integers.

def listc(list1):
    list2=[]
    for i in list1:
        i=i.split(',') #splits the elements of the list
        list2.append(i)
    return(list2) 
list1=list(map(str,input("Enter the elements of list: ").split()))
list3=listc(list1) 
print(list3)
Output for the above program:
Enter the elements of list: abc pqr xyz mno jkl
[['abc'], ['pqr'], ['xyz'], ['mno'], ['jkl']]

Approach 2: Using map()
In the following code, map() method and lambda are used. map() applies the same function to its parameters in every iteration.

def listc(list1):
    list2=list(map(lambda i : [i],list1))
    return list2

list1=list(map(str,input("Enter the elements of list: ").split()))
list3=listc(list1) 
print(list3)
Output of the above program:
Enter the elements of list: rst ijh lmn xyz
[['rst'], ['ijh'], ['lmn'], ['xyz']]

Approach 3: List Comprehension
In the following code, we have used straight forward approach using loop and append() method to create the list of lists.

def listc(list1): 
    list2= [[i] for i in list1]
    return list2

list1=list(map(int,input("Enter the elements of list: ").split()))
list3=listc(list1)
print(list3)
Output for above program:
Enter the elements of list: 2 3 4 5 6 7
[[2], [3], [4], [5], [6], [7]]

2. Convert a list of dictionary to a list of lists:

We need to convert the Python list of dictionary to a list of lists for applications in web development domain. This also helps us to save memory as the key value won’t be repeated again. Below are approaches that can be used to convert list of dictionary to list of lists.

Approach 1: List Comprehension
The following code is a simple list comprehension code where we have used for loop. Firstly, a list of the keys is formed and then its values are formed into a list of lists.

listdict1=[{'a':1,'b':2,'c':3},{'a':6,'b':7,'c':5},{'a':9,'b':13,'c':17}]
list1=[k for k in listdict1[0]]
out1=[list1,*[list(i.values()) for i in listdict1]]
print(out)
Output for the above code:
[['a', 'b', 'c'], [1, 2, 3], [6, 7, 5], [9, 13, 17]]

Approach 2: Using enumerate()
enumerate() takes an iterable and a starting point for enumerating object as input. It adds an automatic counter to the iterable. Here we have used enumerate() to get the index of the dictionary in list of dictionaries.

listdict=[{'a':1,'b':2,'c':3},{'a':6,'b':7,'c':5}]
out=[]
for i, j in enumerate(listdict,start=0):
    if(i == 0):
        out.append(list(j.keys()))
        out.append(list(j.values()))
    else:
        out.append(list(j.values()))
print(out)
Output for the above code:

[[‘a’, ‘b’, ‘c’], [1, 2, 3], [6, 7, 5]]

Approach 3: Using itemgetter()

itemgetter() has functionalities similar to lambda and sorted() but has a different internal implementation. The keys of the dictionary are converted into tuples using itemgetter().

from operator import itemgetter
listdict=[{'a':1,'b':2,'c':3},{'a':6,'b':7,'c':5},{'a':7,'b':11,'c':17},{'a':19,'b':13,'c':9}]

list1=[k for k in listdict[0]]
getdict=itemgetter('a','b','c')

print([list1,[list(getdict(data)) for data in listdict]])

Output for the above code :

[['a', 'b', 'c'], [[1, 2, 3], [6, 7, 5], [7, 11, 17], [19, 13, 9]]]

Leave a Reply

Your email address will not be published. Required fields are marked *