
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Convert list into list of lists in Python
During data analysis we face scenarios to convert every element of a list into a sublist. So in this article we will need to take a normal list as an input and convert into a list of lists where each element becomes a sublist.
Using for loop
This is a very straight forward approach in which we create for loop to read each element. We read it as a list and store the result in the new list.
Example
Alist = ['Mon','Tue','Wed','Thu','Fri'] #Given list print("Given list: ",Alist) # Each element as list NewList= [[x] for x in Alist] # Print print("The new lists of lists: ",NewList)
Output
Running the above code gives us the following result −
Given list: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] The new lists of lists: [['Mon'], ['Tue'], ['Wed'], ['Thu'], ['Fri']]
With split
In this approach we use the split function to extract each of the elements as they are separated by comma. We then keep appending this element as a list into the new created list.
Example
Alist = ['Mon','Tue','Wed','Thu','Fri'] #Given list print("Given list: ",Alist) NewList= [] # Using split for x in Alist: x = x.split(',') NewList.append(x) # Print print("The new lists of lists: ",NewList)
Output
Running the above code gives us the following result −
Given list: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] The new lists of lists: [['Mon'], ['Tue'], ['Wed'], ['Thu'], ['Fri']]
Using map
The map function is used to apply the same function again and again to a sequence of parameters. So we use a lambda function to create a series of list elements by reading each element from the original list and apply map function to it.
Example
Alist = ['Mon','Tue','Wed','Thu','Fri'] #Given list print("Given list: ",Alist) # Using map NewList= list(map(lambda x:[x], Alist)) # Print print("The new lists of lists: ",NewList)
Output
Running the above code gives us the following result −
Given list: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] The new lists of lists: [['Mon'], ['Tue'], ['Wed'], ['Thu'], ['Fri']]
- Related Articles
- Convert a list into tuple of lists in Python
- Python - Convert a list of lists into tree-like dict
- Python - Convert List of lists to List of Sets
- Python program to convert a list into a list of lists using a step value
- Convert list of tuples into list in Python
- How to convert a list of lists into a single list in R?
- Python - Convert given list into nested list
- Convert a string representation of list into list in Python
- Convert list of string into sorted list of integer in Python
- Python - Convert column to separate elements in list of lists
- Convert list of tuples into digits in Python
- Convert a nested list into a flat list in Python
- Convert set into a list in Python
- Convert list of string to list of list in Python
- Convert list of tuples to list of list in Python
