Python | Convert list into list of lists
Given a list of strings, write a Python program to convert each element of the given list into a sublist. Thus, converting the whole list into a list of lists.
Examples:
Input : ['alice', 'bob', 'cara'] Output : [['alice'], ['bob'], ['cara']] Input : [101, 202, 303, 404, 505] Output : [[101], [202], [303], [404], [505]]
Approach #1 : Naive Approach Use another list ‘res’ and a for a loop. Using split() method of Python we extract each element from the list in the form of the list itself and append it to ‘res’. Finally, return ‘res’. One drawback of this method is that it does not work with integer list as ‘int’ object has no attribute ‘split’.
Python3
# Python3 program to convert# list into a list of listsdef extractDigits(lst): res = [] for el in lst: sub = el.split(', ') res.append(sub) return(res) # Driver codelst = ['alice', 'bob', 'cara']print(extractDigits(lst)) |
[['alice'], ['bob'], ['cara']]
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach #2 : List comprehension List comprehension is an efficient approach as it doesn’t make use of extra space. For each element ‘el’ in list, it simply appends [el] to the output list.
Python3
# Python3 program to convert# list into a list of listsdef extractDigits(lst): return [[el] for el in lst] # Driver codelst = ['alice', 'bob', 'cara']print(extractDigits(lst)) |
[['alice'], ['bob'], ['cara']]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Approach #3 : Python map() The given code maps the function el:[el] for each item of the given iterable ‘lst’. Hence outputs each element as a list itself.
Python3
# Python3 program to convert# list into a list of listsdef extractDigits(lst): return list(map(lambda el:[el], lst)) # Driver codelst = ['alice', 'bob', 'cara']print(extractDigits(lst)) |
[['alice'], ['bob'], ['cara']]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as a new list is created to store the transformed elements.
Approach #4: Using List Comprehension with map() function
In this approach, we can combine the use of list comprehension and the map() function to achieve the desired result. The map() function applies the lambda function to each element of the input list ‘lst’, which returns a list with each element enclosed in a list. The list comprehension then iterates over the resulting mapped list and returns the final list of lists.
Step-by-step approach:
- Define a lambda function that takes an element of the input list ‘lst’ and returns it enclosed in a list.
- Use the map() function to apply the lambda function to each element of ‘lst’ and return a list of lists.
- Use a list comprehension to iterate over the resulting list of lists and return the final output list.
- Define the ‘extractDigits’ function that takes the input list ‘lst’ as an argument and returns the output list.
- Call the ‘extractDigits’ function with the input list ‘lst’ as an argument.
- Print the output list returned by the ‘extractDigits’ function.
Python3
def extractDigits(lst): return [x for x in map(lambda el: [el], lst)]lst = ['alice', 'bob', 'cara']print(extractDigits(lst)) |
[['alice'], ['bob'], ['cara']]
Time complexity: O(n), where n is the length of the input list ‘lst’.
Auxiliary space: O(n), where n is the length of the input list ‘lst’.


Please Login to comment...