Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Convert string to a list in Python
Next article icon

Python – Convert list of string to list of list

Last Updated : 27 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, we often encounter scenarios where we might have a list of strings where each string represents a series of comma-separated values, and we want to break these strings into smaller, more manageable lists. In this article, we will explore multiple methods to achieve this.

Using List Comprehension

List comprehension is a concise way to create lists. By combining it with the split() method, which splits a string into a list based on a specified delimiter, we can efficiently transform a list of strings into a list of lists.

Python
a = ["GeeksforGeeks"]  result = [item.split(",") for item in a] print(result) 

Output
[['GeeksforGeeks']] 

Explanation:

  • The split(",") method is applied to each string in the list, splitting it wherever a comma appears.
  • The list comprehension iterates over each string (item) in the list and applies the split() method.
  • The result is a new list, where each string has been converted into a list of substrings.

Let’s explore some more methods and see how we can convert a list of strings to a list of lists.

Table of Content

  • Using map() with split()
  • Using a for Loop
  • Using Regular Expressions with re.split()

Using map() with split()

map() function applies a specified function to each item in an iterable. When combined with the split() method, it can be used to transform a list of strings into a list of lists.

Python
a = ["Learn,Python,with,Gfg", "GeeksforGeeks"]  res = list(map(lambda x: x.split(","), a)) print(res) 

Output
[['Learn', 'Python', 'with', 'Gfg'], ['GeeksforGeeks']] 

Explanation:

  • The map() function takes two arguments: a function (in this case, a lambda function that applies split(",")) and an iterable (the list).
  • Each string in the data list is processed by the split() method, producing a list of substrings.
  • The list() function converts the result of map() into a list.

Using a for Loop

Using a traditional for loop in Python is a simple way to achieve the conversion.

Python
a = ["Learn,Python,with,GFG", "GeeksforGeeks"]  res = [] for item in a:     res.append(item.split(",")) print(res) 

Output
[['Learn', 'Python', 'with', 'GFG'], ['GeeksforGeeks']] 

Explanation:

  • An empty list result is initialized to store the transformed data.
  • The for loop iterates over each string (item) in the list.
  • The split(",") method is applied to each string, and the resulting list is appended to the result list.

Using Regular Expressions with re.split()

If the delimiter is more complex (e.g., multiple delimiters or patterns), regular expressions provide a powerful alternative. The re.split() function can split strings based on patterns rather than fixed delimiters.

Python
import re  a = ["Learn|Python|with|GFG", "Geeks|for|Geeks"]  res = [re.split(r"\|", item) for item in a] print(res) 

Output
[['Learn', 'Python', 'with', 'GFG'], ['Geeks', 'for', 'Geeks']] 

Explanation:

  • The re.split() function splits strings based on a regular expression pattern. Here, the pattern r"\|" matches the pipe (|) character.
  • List comprehension is used to apply re.split() to each string in the data list.
  • The result is a list of lists where each string is split based on the specified pattern.


Next Article
Convert string to a list in Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python list-programs
  • python-list
Practice Tags :
  • python
  • python-list

Similar Reads

  • Convert string to a list in Python
    Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
    2 min read
  • Python - Converting list string to dictionary
    Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val
    3 min read
  • Convert Object to String in Python
    Python provides built-in type conversion functions to easily transform one data type into another. This article explores the process of converting objects into strings which is a basic aspect of Python programming. Since every element in Python is an object, we can use the built-in str() and repr()
    2 min read
  • Converting all Strings in a List to Integers - Python
    We are given a list of strings containing numbers and our task is to convert these strings into integers. For example, if the input list is ["1", "2", "3"] the output should be [1, 2, 3]. Note: If our list contains elements that cannot be converted into integers such as alphabetic characters, string
    2 min read
  • Python - Convert Dictionary Object into String
    In Python, there are situations where we need to convert a dictionary into a string format. For example, given the dictionary {'a' : 1, 'b' : 2} the objective is to convert it into a string like "{'a' : 1, 'b' : 2}". Let's discuss different methods to achieve this: Using strThe simplest way to conve
    2 min read
  • Convert Set to String in Python
    Converting a set to a string in Python means changing a group of unique items into a text format that can be easily read and used. Since sets do not have a fixed order, the output may look different each time. For example, a set {1, 2, 3} can be turned into the string "{1, 2, 3}" or into "{3, 1, 2}"
    3 min read
  • Convert a List of Characters into a String - Python
    Our task is to convert a list of characters into a single string. For example, if the input is ['H', 'e', 'l', 'l', 'o'], the output should be "Hello". Using join() We can convert a list of characters into a string using join() method, this method concatenates the list elements (which should be stri
    2 min read
  • Convert List Of Tuples To Json Python
    Working with data often involves converting between different formats, and JSON is a popular choice for data interchange due to its simplicity and readability. In Python, converting a list of tuples to JSON can be achieved through various approaches. In this article, we'll explore four different met
    3 min read
  • Convert String to Set in Python
    There are multiple ways of converting a String to a Set in python, here are some of the methods. Using set()The easiest way of converting a string to a set is by using the set() function. Example 1 : [GFGTABS] Python s = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s
    1 min read
  • Convert Lists to Comma-Separated Strings in Python
    Making a comma-separated string from a list of strings consists of combining the elements of the list into a single string with commas between each element. In this article, we will explore three different approaches to make a comma-separated string from a list of strings in Python. Make Comma-Separ
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences