- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to convert a list into a list of lists using a step value
When it is required to convert a list into a list of lists using a step value, a method is defined that uses a simple iteration, the ‘split’ method and the ‘append’ method.
Below is a demonstration of the same
def convert_my_list(my_list): my_result =  for el in my_list: sub = el.split(', ') my_result.append(sub) return(my_result) my_list = ['peter', 'king', 'charlie'] print("The list is :") print(my_list) print("The resultant list is :") print(convert_my_list(my_list))
The list is : ['peter', 'king', 'charlie'] The resultant list is : [['peter'], ['king'], ['charlie']]
A method named ‘convert_my_list’ is defined.
It takes a list as a parameter.
Inside it, an empty list is defined.
The list is iterated over, and split based on comma.
This list items are appended to the empty list.
Outside the method, a list is defined and is displayed on the console.
The method is called by passing this list.
The output is displayed on the console.
- Convert a list into tuple of lists in Python
- Convert list into list of lists in Python
- How to convert a list of lists into a single list in R?
- Python - Convert a list of lists into tree-like dict
- Python program to convert a list of characters into a string
- Python program to convert a list of tuples into Dictionary
- Convert a nested list into a flat list in Python
- Convert a string representation of list into list in Python
- Python - Convert List of lists to List of Sets
- Convert a list into a tuple in Python.
- C# program to convert a list of characters into a string
- Java program to convert a list of characters into a string
- Convert set into a list in Python
- How to convert a list into a tuple in Python?
- Java Program to convert Properties list into a Map