Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

How to flatten list of lists in Python?

In Python, a list of lists (or cascaded lists) resembles a two-dimensional array - although Python doesn't have a concept of the array as in C or Java. Hence, flattening such a list of lists means getting elements of sublists into a one-dimensional array-like list. For example, a list [[1,2,3],[4,5,6]]is flattened into [1,2,3,4,5,6].

This can be achieved by different techniques, each one of them is explained below:

Flatten List using Nested for Loop

Using the nested for loop to append each element of sublist in a flat list

Example:
nestedlist = [ [1, 2, 3, 4], ["Ten", "Twenty", "Thirty"], [1.1,  1.0E1, 1+2j, 20.55, 3.142]] flatlist=[] for sublist in nestedlist: for element in sublist: flatlist.append(element) print(flatlist)
Output
[1, 2, 3, 4, 'Ten', 'Twenty', 'Thirty', 1.1, 10.0, (1+2j), 20.55, 3.142]

Flatten List using List Comprehension

List comprehension technique gives the same result in a single compact statement.

Example:
nestedlist = [ [1, 2, 3, 4], ["Ten", "Twenty", "Thirty"], [1.1,  1.0E1, 1+2j, 20.55, 3.142]] flatlist=[element for sublist in nestedlist for element in sublist] print(flatlist)
Output
[1, 2, 3, 4, 'Ten', 'Twenty', 'Thirty', 1.1, 10.0, (1+2j), 20.55, 3.142]

Flatten List using the reduce() Function of functools Module

The first argument to the reduce() function is a function itself with two arguments, and the second argument is a list. Argument function is applied cumulatively to elements in the list. For example,reduce(lambda a,b:a+b, [1,2,3,4,5])returns cumulative sum of numbers in the list.

Example:
from functools import reduce flatlist = reduce(lambda a,b:a+b, nestedlist) print(flatlist)
Output
[1, 2, 3, 4, 'Ten', 'Twenty', 'Thirty', 1.1, 10.0, (1+2j), 20.55, 3.142]

Recall that the + symbol is defined as a concatenation operator for sequence data types (list, tuple, and string). Instead of a lambda function, we can also use the concat() function defined in the operator module as an argument to cumulatively append the sublists to flatten.

Example:
from functools import reduce from operator import concat nestedlist = [ [1, 2, 3, 4], ["Ten", "Twenty", "Thirty"], [1.1,  1.0E1, 1+2j, 20.55, 3.142]] flatlist = reduce(concat, nestedlist) print(flatlist)
Output
[1, 2, 3, 4, 'Ten', 'Twenty', 'Thirty', 1.1, 10.0, (1+2j), 20.55, 3.142]
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.