Lists are one of the most commonly used data types in Python and are used to store multiple values in a single variable. A list of Lists is a nested list where each element in a list is a list itself. Flattening the list of lists means converting the 2D list into a single dimension list.

There are various ways to flatten a list or list of lists. Let’s dive in to see how you can flatten a list or a list of lists in Python.

Also read: How to create a list in Python?

# What is a list of lists?

A list of Lists or 2D list is a nested list where each element in a list is a list itself. For example:

`list1 = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]`

Here, in the above example, ‘list1’ is a list, and each element in list1 is itself a list. Thus, list1 is a list of lists.

A list has a definite count, and all the elements in a list are indexed, with zero being the first index. Each element in a list is referred through its index number.

There are two types of list of lists: Regular and irregular. In a Regular list of lists, each element of that list is a sublist, whereas, in an Irregular list of lists, the element in the list can be a sublist or single element. For example:

```Regular list of lists
list1 = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

Irregular list of lists
list2 = [['a', 'b'], 'c', ['d', 'e', 'f'], ['g', 'h', 'i', 'j', 'k'], 'l']```

# Flatten list of lists Using loops

You can use loops to flatten the list of lists.

## Flattening Regular list of lists

See the example code given below to find out how you can use loops to flatten a regular list of lists.

``````list1= [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
final_list = []

print("The original list is: ",list)

# iterate over the data using for loop
for element in list1:
final_list += element

# printing the output - flatten list
print("The flatten list is: ", final_list)``````

The output of the above code is:

```The original list is: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
The flatten list is: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']```

## Flattening irregular list of lists

Check out the example code given below to find out how you can use loops to flatten an irregular list of lists.

``````list1 = [1, 2, 3, [4, 5], 6, [, 8]]
final_list = []

print("original list is: ", list1)

def flat_list(list1):
# using for loop to iterate over the list
for element in list1:
if type(element) == list:
# recursively calling the same function with current element as new argument if the element is a list
flat_list(element)
else:
final_list.append(element)

# calling the method to flatten the list
flat_list(list1)

# printing the final_list
print("flattened list is: ",final_list)``````

The output of the above code is:

```original list is: [1, 2, 3, [4, 5], 6, [. 8]]
flattened list is: [1, 2, 3, 4, 5, 6, 7, 8]```

# Using NumPy

NumPy is a package in Python that provides you with various methods and operations to apply on arrays. Using the concatenate() and flat() method from the NumPy package, you can flatten your list of lists into a list. See the example code given below:

``````import numpy

list1= [[1, 2], [3, 4], [5, 6, 7]]
final_list = list(numpy.concatenate(list1).flat)

print('Original list is: ', list1)
print('Flattened list is: ', final_list)``````

The output of the above code is:

```Original list is: [[1, 2], [3, 4], [5, 6. 7]]
Flattened list is: [1, 2, 3, 4, 5, 6, 7]```

# Using Itertools package

The Itertools package in Python has a method known as chain, which iterates over the list and returns the elements as an iterable, which you then have to convert into a list. Check the example code given below:

``````print("Original list is: ",list1)

# flattening the list using chain() method
final_list = list(itertools.chain(*list1))

print("Flattened list is: ",final_list)``````

The output of the above code is:

```Original list is: [[1, 2, 3, 4], [5, 6], [7, 8, 9]]
Flattened list is: [1, 2, 3, 4, 5, 6, 7, 8, 9]```

Using any one of the above-given ways, you can flatten your list of lists into a single list.

## Read more from Candid.Technology

Copyright © 2018-2022 Candid.Technology | Candid Today Press Pvt. Ltd.​