Python – List of Lists to List of Strings. How?

5/5 - (2 votes)

Coding Challenge

πŸ’¬ Coding Challenge: Given a nested Python list, i.e., a list of lists. How to convert it to a string list, so that each inner list becomes a single string?

Consider the following examples:

1. 
Input: 
[['h', 'e', 'l', 'l', 'o'], ['w', 'o', 'r', 'l', 'd']]
Output: 
['hello', 'world']

2. 
Input: 
[['h', 'i'], ['!']]
Output: 
['hi', '!']

3. 
Input: 
[['h', 'i'], []]
Output: 
['hi', '']

Method 1: Joining Inner Lists Using List Comprehension

The Python one-liner expression [''.join(inner) for inner in lst] combines a simple list comprehension statement with the string.join() method to combine an inner list into a single string and repeat this for each inner list.

Here’s a code example:

def f(lst):
    '''Converts input list of lists of strings
       to list of strings.'''
    return [''.join(inner) for inner in lst]


# Example 1:
lst = [['h', 'e', 'l', 'l', 'o'], ['w', 'o', 'r', 'l', 'd']]
print(f(lst))
# ['hello', 'world']

# Example 2:
lst = [['h', 'i'], ['!']]
print(f(lst))
# ['hi', '!']

# Example 3:
lst = [['h', 'i'], []]
print(f(lst))
# ['hi', '']


You may want to watch the following background video on list comprehension that will answer all your questions about this exciting Python feature:

A Simple Introduction to List Comprehension in Python

πŸ‘‰ Recommended Tutorial: Python List Comprehension

Method 2: Nested For Loop and String Concatenation

A very clunky way to convert a nested list of strings to a string list is to construct each string at a time using two nested for loops.

  • The outer loop iterates over all lists in the nested list.
  • The inner loop iterates over all strings and aggregates those strings in a single larger string using the string concatenation operator +=.

You collect each of those constructed strings in a variable that points to an initially empty list using the list.append() method.

Here’s a code example:

def f(lst):
    '''Converts input list of lists of strings
       to list of strings.'''
    tmp = []
    for inner in lst:
        s = ''
        for x in inner:
            s += x
        tmp.append(s)
    return tmp


# Example 1:
lst = [['h', 'e', 'l', 'l', 'o'], ['w', 'o', 'r', 'l', 'd']]
print(f(lst))
# ['hello', 'world']

# Example 2:
lst = [['h', 'i'], ['!']]
print(f(lst))
# ['hi', '!']

# Example 3:
lst = [['h', 'i'], []]
print(f(lst))
# ['hi', '']


πŸ‘‰ Recommended Tutorial: You should definitely check out our tutorial on string concatenation which indicates that it is not super efficient because strings are immutable and you need to create a new string in each iteration.

Method 3: Mapping Each Inner List to a Concatenated String

Another great way to concatenate the strings of each inner list to a single string is list(map(''.join, lst)). You pass the ''.join method on the empty string '' into the map() function that then applies this method to each element in an iterable lst that you pass as a second argument.

Here’s a code example:

def f(lst):
    '''Converts input list of lists of strings
       to list of strings.'''
    return list(map(''.join, lst))


# Example 1:
lst = [['h', 'e', 'l', 'l', 'o'], ['w', 'o', 'r', 'l', 'd']]
print(f(lst))
# ['hello', 'world']

# Example 2:
lst = [['h', 'i'], ['!']]
print(f(lst))
# ['hi', '!']

# Example 3:
lst = [['h', 'i'], []]
print(f(lst))
# ['hi', '']


Note that we need to wrap the output of the map() function into a list() constructor to create a list from the iterable object. The return value of the map() function is the iterable map object that doesn’t look very pretty to us humans.

In any case, you should definitely check out this video on the map() function that will teach you all you need to know!

Mastering the Python Map Function [+Video]

πŸ‘‰ Recommended Tutorial: Python map() β€” Finally Mastering the Python Map Function

Summary

You have learned that the following methods solve your problem X

  • Method 1: [''.join(inner) for inner in lst]
  • Method 2: nested for loops
  • Method 3: list(map(''.join, lst))

πŸ’‘ Conclusion: List comprehension (Method 1) is the best and most Pythonic way to convert a nested list to a string list because it is the easiest to use, more concise than the nested for loop (Method 2), and doesn’t require complicated nested function calls (Method 3).

That’s my opinion anyway… πŸ™‚