- 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
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
- Related Articles
- Convert a list into tuple of lists in Python
- Convert list into list of lists in Python
- Python - Convert a list of lists into tree-like dict
- How to convert a list of lists into a single list in R?
- Python program to convert a list of tuples into Dictionary
- Python - Convert List of lists to List of Sets
- Convert a nested list into a flat list in Python
- Convert a string representation of list into list in Python
- Java program to convert a list of characters into a string
- C# 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
- Python program to convert a list of strings with a delimiter to a list of tuple
- Python Program to Convert a given Singly Linked List to a Circular List