Hướng dẫn python flat list
IntroductionA list is the most flexible data structure in Python. Whereas, a 2D list which is commonly known as a list of lists, is a list object where every item is a list itself - for example: Show Flattening a list of lists entails converting a 2D list into a 1D list by un-nesting each list item stored in the list of lists - i.e., converting The process of flattening can be performed using nested for loops, list comprehensions, recursion, built-in functions or by importing libraries in Python depending on the regularity and depth of the nested lists. Types of Nested ListsSince Python is weakly typed, you can encounter regular and irregular lists of lists. Regular List of ListsEvery element of this list is a sublist, thereby adhering to the uniformity of the element type. Irregular List of ListsEvery element of this list is either a sublist or a non-list item (for example an integer or string). Therefore, there is an irregularity in terms of the element type. Example: Flatten List of Lists Using Nested for LoopsThis is a brute force approach to obtaining a flat list by picking every element from the list of lists and putting it in a 1D list. The code is intuitive as shown below and works for both regular and irregular lists of lists:
This results in:
Flatten List of Lists Using a List ComprehensionThis approach provides an elegant but a less intuitive solution to create a flat list based on an existing 2D list:
Which would output the following:
Flatten List of Lists RecursivelyThe 2D list can be flattened recursively as well. The implementation below works both for regular and irregular list of lists:
Which would give us:
Using LibrariesYou can also rely on the help of Pyhon libraries for this task. Flatten List of Lists Using functools (reduce() and iconcat())The
Which would give us the desired result:
Flatten List of Lists Using itertools (chain())This approach is ideal for transforming a 2-D list into a single flat list as it treats consecutive sequences as a single sequence by iterating through the iterable passed as the argument in a sequential manner.
Again, this would give us a flattened list as the output:
Flatten List of Lists Using numpy (concatenate() and flat())Numpy offers common operations which include concatenating regular 2D arrays row-wise or column-wise. We are also using the
Which gives us the desired output:
Using Built-In FunctionsThe task of flattening can also be performed by using built-in functions that Python offers. Flatten List of Lists Using sumSumming over inner lists is another solution. The function has
two parameters: This approach is convenient as you don't have to import anything but it's slower than Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
With the output:
Flatten List of Lists Using LambdaAn anonymous function can be defined using the lambda keyword. The regular/irregular list is passed as the argument to this anonymous function and the evaluation of the expression is done to obtain a flat 1D list:
We would again get the desired result:
ConclusionIn this article, we provided an extensive list of ways we can complete the task of flattening a list of lists in Python. |