Flat list python – Python : Convert List of Lists or Nested List to Flat List

Flat list python: Lists are similar to dynamically sized arrays declared in other languages (for example, vector in C++ and ArrayList in Java). Lists do not always have to be homogeneous, which makes them a very useful tool in Python. A single list can contain DataTypes such as Integers, Strings, and Objects. Lists are mutable, which means they can be modified after they are created.

A list of lists is a list object in Python, with each list element being a separate list.

Example:

Input:

listoflists = [ [3 ,6 ,7 ] , [1 , 2, 3 ] , [8 , 7, 6 ] ]

Output:

[3, 6, 7, 1, 2, 3, 8, 7, 6]

Nested Lists to Flat list Conversion

Convert list of lists to list python: There are several ways to convert nested list to flat list some of them are:

Method#1:Using extend() function

Extend in Python:

The extend() method appends to the end of a list all the elements of an iterable (list, tuple, string, etc.). All iterable elements are added to the end of list1 in this case.

It adds all the contents of the given iterable to the existing list object. Let’s put this to use and convert a list of lists to a flat list.

Approach:

  • Take a empty list say flatlist
  • Traverse the given list of lists, and for each list within it, extend that list to flatlist.
  • print flatlist

Below is the implementation:

# Function to convert list of lists to flat list
def convertToFlatList(listoflists):
    # Taking empty flat list
    FlatList = []
    # Traverse the given listoflists
    for element in listoflists:
        # Adding this list to flatlist using extend
        FlatList.extend(element)

        # returning the flatlist
    return FlatList


# Driver code
# given listoflists
listoflists = [[3, 6, 7], [1, 2, 3], [8, 7, 6]]
# passing listoflists to convertToFlatList function
print(convertToFlatList(listoflists))

Output:

[3, 6, 7, 1, 2, 3, 8, 7, 6]

Method #2:Using append() function

Append():

Python’s append() method adds a single item to an existing list. It does not return a new list of items, but rather modifies the original list by appending the item to the end. The size of the list increases by one after executing the method append on it.

Approach:

  • Take a empty list say flatlist
  • Traverse the given list of lists, and then traverse it again using a loop for each list within it, appending all the elements to flatlist.
  • print flatlist.

Below is the implementation:

# Function to convert list of lists to flat list
def convertToFlatList(listoflists):
    # Taking empty flat list
    FlatList = []
    # Traverse the given listoflists
    for element in listoflists:
        # Traverse the inner list
        for loopelement in element:
          # appeend this loopelement
            FlatList.append(loopelement)

        # returning the flatlist
    return FlatList


# Driver code
# given listoflists
listoflists = [[3, 6, 7], [1, 2, 3], [8, 7, 6]]
# passing listoflists to convertToFlatList function
print(convertToFlatList(listoflists))

Output:

[3, 6, 7, 1, 2, 3, 8, 7, 6]

Method #3:Using List Comprehension

We’ll use list comprehension to iterate over a list of lists, then iterate over the individual elements in each internal list. Then, in a new list, add those elements.

Below is the implementation:

# Function to convert list of lists to flat list
def convertToFlatList(listoflists):
    # Using list Comprehension
    FlatList = [item for element in listoflists for item in element]

    # returning the flatlist
    return FlatList


# Driver code
# given listoflists
listoflists = [[3, 6, 7], [1, 2, 3], [8, 7, 6]]
# passing listoflists to convertToFlatList function
print(convertToFlatList(listoflists))

Output:

[3, 6, 7, 1, 2, 3, 8, 7, 6]

Related Programs:

Leave a Comment