# Convert 2d List to String in Python

A 2-dimensional (2D) list is simply a list of lists (lists within a list). Here are some examples of 2D lists in Python.

In this article, we will discuss how to convert a 2D list into a Python string using different methods (later, we will test which of the methods is the fastest). All the methods discussed here will do the job in two steps:

• Flatten the list, that is, convert the 2D list into a 1D list, and
• Join the elements of the 1D array using the str.join() method.

## The str.join() method

We will use this method for all the methods we will discuss. The str.join(iterable) is used to join the elements of the iterable (in our case, a 1D list) on str. Here is an example,

Output:

`apple*pineapple*grape`

Note that the elements of the iterable in str.join() must be string otherwise, the function will raise an error. We will deal with that in Method 2.

Let’s now discuss some methods to convert a 2D Python list into a string.

## Method 1: Using for-loop or List Comprehension with str.join() function

There are two ways to use for-loop her. The first way is to work with nested for-loop, as shown below.

Output:

```['Apple', 'Orange', 'Cow', 'Goat', 'Chicago', 'Seattle', 'Nebraska']
Apple Orange Cow Goat Chicago Seattle Nebraska```

Alternatively, you can use the extend function to add elements of a sublist to the flat_list at once without looping over items individually.

Output:

```['Apple', 'Orange', 'Cow', 'Goat', 'Chicago', 'Seattle', 'Nebraska']
Apple Orange Cow Goat Chicago Seattle Nebraska```

The for-loop approach can be reduced to a one-liner using the concept of list comprehension, as shown in the following example.

Output:

`Apple#Orange#Cow#Goat#Chicago#Seattle#Nebraska`

## Method 2: Using itertools + str.join()

There are two functions in itertools that you can do to flatten a 2D list – itertools.chain() and itertools.chain.from_iterable(). Here are the examples,

Output:

```['Python', 'Java', 'Functions', 'Classes', 7, 8, 9]
['Python', 'Java', 'Functions', 'Classes', '7', '8', '9']
Python Java Functions Classes 7 8 9```

The map(func, iterable) is a function efficiently used to apply function func in all the elements of the iterable. In the example above, we used it to convert each element of lst1d into a string.

The next example goes through the same steps as the previous one but now uses itertools.chain.from_iterable() instead of itertools.chain().

Output:

`3--4--5--6--7--8`

## Method 3: Using the inbuilt sum() function and str.join()

Recall that the sum of two lists, A and B (say), in Python is equal to a list of all the elements in A and B, e.g., if A= [e1, e3] and B = [e2, e4], then A+B = [e1, e3, e2, e4]. We can also implement the addition with the sum(iterable, 0) function.

In the sum(iterable, 0) function, the first element is an iterable we want to sum (in our case, the 2D list), and the second element is the initial value of the sum (0 by default). We must change the initial value to an empty list because we will be “adding” lists. In that case, the sum([[1,3], [2,4]], []) will be [1, 3, 2, 4].

Here is a code example.

Output:

```['Apple', 'Orange', 'Cow', 'Goat', 'Chicago', 'Seattle', 'Nebraska']

## Method 4: Using functools and operator modules

Output:

`Apple;Orange;Cow;Goat;Chicago;Seattle;Nebraska`

## Method 5: Using NumPy Methods

We can use two methods here: numpy.concatenate(2d_list) or array.flat. The first method works when the sublists of our 2D list are of varying sizes, but the latter requires the sublists to be the same size. Let’s see some examples.

Output:

`Python Java Functions Classes 7 8 9`

Output:

`Python Java Functions Classes 7 8`

## Which of the Methods is the Fastest?

The following code snippet can be used to test the speed of all the methods we discussed above using perfplot – a function that extends Python’s timeit functionality in conducting speed tests.

The benchmarks show that Method 4 (using functools and operator) is the fastest, followed by methods using itertools (Method 2). The sum method (Method 3) starts well, but as the size of the list increases, its performance deteriorates significantly.

On the other hand, Numpy Methods (see Method 5) are not the fastest, but they have a linear increase in runtime. I suspect that for a huge input list, NumPy will be the best (I didn’t test this).

## Conclusion

This guide discussed different methods of converting a 2D Python list into a string. We also tested the runtime speed for each method and found that Method 4 (Using functools and operator methods) is the fastest, followed by the methods using itertools (see Method 2).

The sum function discussed in Method 3 does well for a small input list, but it gets slow so fast as the input size grows.