w3resource

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

Python map: Exercise-16 with Solution

Write a Python program to convert a given list of strings into a list of lists using the map function.

Sample Solution:

Python Code:

# Define a function named 'strings_to_listOflists' that takes a list of strings as input
def strings_to_listOflists(str):
    # Use the 'map' function with 'list' to convert each string into a list of its individual characters
    result = map(list, str)
    # Convert the map object to a list and return the result
    return list(result)

# Define a list of strings named 'colors'
colors = ["Red", "Green", "Black", "Orange"]

# Print a message indicating the operation to be performed
print('Original list of strings:')

# Print the original list of strings
print(colors)

# Print a newline for better readability
print("\n")

# Print a message indicating the operation to be performed
print("Convert the said list of strings into list of lists:")

# Print the result of calling the 'strings_to_listOflists' function with the 'colors' list
print(strings_to_listOflists(colors))

Sample Output:

Original list of strings:
['Red', 'Green', 'Black', 'Orange']

Convert the said list of strings into list of lists:
[['R', 'e', 'd'], ['G', 'r', 'e', 'e', 'n'], ['B', 'l', 'a', 'c', 'k'], ['O', 'r', 'a', 'n', 'g', 'e']]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to split a given dictionary of lists into list of dictionaries using map function.

Next: Write a Python program to convert a given list of tuples to a list of strings using map function.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/map/python-map-exercise-16.php