Write a Python program to convert the list of string to list of list

Python | Convert list of string to list of list

Many times, we come over the dumped data that is found in the string format and we require it to be represented into the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to list to perform tasks is quite common in web development. Let’s discuss certain ways in which this can be performed.

Method #1 : Using strip() + split()
A combination of strip and split function can perform a particular task. The strip function can be used to get rid of the brackets and split function can make the data list comma-separated.




# Python3 code to demonstrate
# to convert list of string to list of list
# using strip() + split()
# initializing list
test_list = [ '[1, 4, 5]', '[4, 6, 8]' ]
# printing original list
print ("The original list is : " + str(test_list))
# using strip() + split()
# to convert list of string to list of list
res = [i.strip("[]").split(", ") for i in test_list]
# printing result
print ("The list after conversion is : " + str(res))

Output :

The original list is : ['[1, 4, 5]', '[4, 6, 8]'] The list after conversion is : [['1', ' 4', ' 5'], ['4', ' 6', ' 8']]

Method #2 : Using list slicing and split()
The task performed in the above method can also be performed using the list slicing in which we slice all the elements from second to second last element hence omitting the last brackets.




# Python3 code to demonstrate
# to convert the list of string to list of list
# using list slicing + split()
# initializing list
test_list = [ '[1, 4, 5]', '[4, 6, 8]' ]
# printing original list
print ("The original list is : " + str(test_list))
# using list slicing + split()
# to convert list of string to list of list
res = [i[1 : -1].split(', ') for i in test_list]
# printing result
print ("The list after conversion is : " + str(res))

Output :

The original list is : ['[1, 4, 5]', '[4, 6, 8]'] The list after conversion is : [['1', ' 4', ' 5'], ['4', ' 6', ' 8']]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article

Python | Program to convert String to a List

In this program, we will try to convert a given string to a list, where spaces or any other special characters, according to the users choice, are encountered. To do this we use the split() method.
Syntax:

string.split("delimiter")

Examples:

Input : "Geeks for Geeks" Output : ['Geeks', 'for', 'Geeks'] Input : "Geeks-for-Geeks" Output : ['Geeks', 'for', 'Geeks']

The split method is used to split the strings and store them in the list. The built-in method returns a list of the words in the string, using the “delimiter” as the delimiter string. If a delimiter is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
Example 1:




# Python code to convert string to list
def Convert(string):
li = list(string.split(" "))
return li
# Driver code
str1 = "Geeks for Geeks"
print(Convert(str1))

Output:

['Geeks', 'for', 'Geeks']

Example 2:






# Python code to convert string to list
def Convert(string):
li = list(string.split("-"))
return li
# Driver code
str1 = "Geeks-for-Geeks"
print(Convert(str1))

Output:

['Geeks', 'for', 'Geeks']

Example 3:




# Python code to convert string to list character-wise
def Convert(string):
list1=[]
list1[:0]=string
return list1
# Driver code
str1="ABCD"
print(Convert(str1))

Output:

['A','B','C','D']

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




Article Tags :
Python
Python list-programs
Python string-programs
python-list
python-string
Practice Tags :
python-list
Read Full Article

Introduction

While programming we may need to convert a string to list in Python. That could be for any other reason. But, a question arises here, how can we convert a string to different forms of lists?

So, here in this tutorial, we are going to learn how we can convert a string into a list in Python.

Convert string to list in Python

In this short tutorial, find how to convert string to list in Python. We look at all the ways you can achieve this along with their pros and cons.

Convert list of string to list of list in Python

PythonServer Side ProgrammingProgramming

In this article we will see how to create a list of lists which contain string data types. The inner list themselves or of string data type and they may contain numeric or strings as their elements.

Python: Convert a given list of strings into list of lists using map function

Last update on December 29 2020 12:44:33 (UTC/GMT +8 hours)

Method 1: Convert String to list of strings in Python

Syntax:

string.split(separator, maxsplit)

Parameters:

  • Separator: separator to use when splitting the string
    • Default value: whitespace
  • maxsplit: number of splits required
str1 = "Python pool for python knowledge" list1 = list(str1.split(" ")) print(list1)

Output:

['Python', 'pool', 'for', 'python', 'knowledge']

The split method by default takes whitespace as delimiter and separates the words of the string from by the whitespace and converts them into a list.

Method 2: Convert String to list of characters in Python

To convert a string into list of characters, we can simply use type conversion using the inbuilt list() method.

Syntax:

list(iterable)

Parameter:

  • iterable: an object that could be a sequence/collection/any iterator object
str1 = "Python pool for python knowledge" list1 = list(str1) print(list1)

Output:

['P', 'y', 't', 'h', 'o', 'n', ' ', 'p', 'o', 'o', 'l', ' ', 'f', 'o', 'r', ' ', 'p', 'y', 't', 'h', 'o', 'n', ' ', 'k', 'n', 'o', 'w', 'l', 'e', 'd', 'g', 'e']

Using type conversion with the help of list() method, it directly converts the given string into a list of characters for us.

4 Ways to Convert List to String Python: Join, Traversal & More

By SimplilearnLast updated on Dec 8, 202114381549

Table of Contents

View More

Python is one of the most popular programming languages today, and in this tutorial we will learn various nuances of Python, including what is a list in python, what is a string, ways to change a list to a string and more. Let’s start.

What Is a List in Python?

A list in python is an ordered sequence that can hold a variety of object types, such as, integer, character or float. A list in python is equivalent to an array in other programming languages. It is represented using square brackets, and a comma(,) is used to separate two objects present in the list.

Python Training Course

Learn Data Operations in PythonExplore Course

A list and an array in other programming languages differ in the way that an array only stores a similar data type, meaning that an array is homogeneous in nature, but a list in python can store different data types at a time, and therefore it can either be homogeneous or heterogeneous. Below are some examples of homogeneous and heterogeneous lists in python:

Homogenous Lists:

Heterogeneous Lists:

Accessing an Item From the List

An item from the list can be accessed by referring to its index in the list. The indexing of elements in the list starts from 0. Let’s take an example of the list we created in the last step.

To access an element from the list, we pass the index of that element in the print function.

As mentioned earlier, that indexing starts from 0, so when index [1] is passed, it gives the result as “dog”. Similarly, if we pass the index, say [2], it will give the output 2.2

Free Course: Python for Beginners

Master the fundamentals of PythonEnroll Now

How to convert List to String | String to List – Python Program

By
Great Learning Team
-
Apr 6, 2020
0
Share
Facebook
Twitter
WhatsApp
  1. What are Lists
  2. What are Strings
  3. Convert List to Strings
  4. Convert List of integers to a single integer
  5. Convert String to Lists
  6. Convert String to list of characters
  7. Convert List of characters into string

Video liên quan

Postingan terbaru

LIHAT SEMUA