
How to Flatten a List in Python - Interactive Tutorial
Table of content
How to flatten a list of lists in Python
How to flatten a list of lists with a for loop
Using a comprehension to flatten a list of lists
Chaining iterables with itertools.chain()
Concatenating lists with functools.reduce()
Using sum() to concatenate lists
Considering performance when flattening your lists
Flattening Python lists for data science with NumPy
Conclusion
Frequently asked questions
Share with
How to flatten a list of lists in Python
Using a for loop
for loop. This manual approach is simple to understand and implement.- Easy to read and understand.
- Does not require external libraries.
- Can be inefficient for very large lists due to its time complexity.
List comprehension
for loops.- More concise and elegant code.
- Often better performance than
forloops.
- Can be less readable for beginners.
Using itertools
itertools provides a convenient method called chain that can be used to flatten lists.- Very efficient for large lists.
- Uses a well-optimized and proven feature of the standard library.
- May require an additional import, which adds a slight cognitive load.
Using NumPy
NumPy library offers a method for flattening lists, although it is more suited for multi-dimensional arrays.- Extremely fast and efficient for numerical data operations.
- NumPy is optimized for bulk operations.
- Requires installation of the NumPy library.
- Less suitable if you do not regularly work with numerical data.
Method comparison
| Method | Simplicity | Performance | Dependency |
|---|---|---|---|
for loop | High | Average | None |
| List comprehension | Medium | Good | None |
itertools.chain | Medium | Very good | Iterative |
NumPy | Medium | Excellent | NumPy |
How to flatten a list of lists with a for loop
for loop to flatten a list of lists is a straightforward and accessible method, particularly useful for beginners in Python programming. Although this approach may seem basic, it allows understanding the fundamentals of list manipulation and offers complete control over the flattening process.Implementation with a for loop
for loops. The first loop iterates over each sub_list in list_of_lists, and the second loop iterates over each element in that sub_list, adding each element to flattened_list.Advantages of using a for loop
- Clarity and readability: The code is easy to read and understand, making it ideal for beginners. The step-by-step logic is clear, which helps grasp how lists can be manipulated in Python.
- No external dependencies: This method uses only built-in Python features, without requiring additional libraries.
- Explicit control: You have complete control over the flattening process, allowing you to easily add checks or additional transformations on the elements of the list if necessary.
Disadvantages of using a for loop
- Efficiency: For very large lists, this method can become inefficient. Nested loops increase time complexity, and this can lead to significant slowdowns if the data volume is large.
- Longer code: Compared to list comprehensions or library functions like
itertools.chain, this method requires more lines of code, which may be less elegant.
Potential optimization
for loop is quite basic, it can be optimized in some cases. For example, if the sub-lists are very large, you might consider pre-allocating memory for flattened_list if the total size is known in advance, though this is rarely necessary in Python.Use cases
- Simplicity and readability are more important than performance.
- You want to perform additional operations on each element during flattening (e.g., filtering certain elements or applying a transformation).
- You are working with lists of moderate size where performance is not a major concern.
Using a comprehension to flatten a list of lists
for loops, making it a popular choice among Python developers.What is a list comprehension?
Implementation to flatten a list of lists
sub_list in list_of_lists, and the second to iterate over each element in that sub_list.Advantages of list comprehensions
- Conciseness: List comprehensions allow reducing the number of lines of code needed to flatten a list, making the code more compact.
- Performance: Generally, list comprehensions are better optimized than traditional
forloops in Python, which can lead to performance gains, especially with large lists. - Improved readability: For users familiar with the syntax, list comprehensions can be more readable and intuitive, as they express the entire logic in a single line.
Disadvantages of list comprehensions
- Syntactical complexity: For beginners, the syntax can be difficult to understand at first, especially if it includes conditions or nested loops.
- Less flexibility: Compared to
forloops, it can be more challenging to add transformations or complex conditions without sacrificing readability.
Comparison with for loops
| Criterion | List comprehension | for loop |
|---|---|---|
| Conciseness | Very high | Medium |
| Performance | Good to very good | Medium |
| Readability | Good (for experienced users) | Very good (especially for beginners) |
| Flexibility | Medium | High |
Example with condition
if element > threshold condition to include only elements greater than the specified threshold.Conclusion
for loops may still be preferred for their simplicity and flexibility.Chaining iterables with itertools.chain()
itertools.chain() function from the standard Python library is a powerful method for flattening a list of lists. This function allows concatenating multiple iterables, thus providing an efficient solution to the flattening problem.What is itertools.chain()?
itertools.chain() is a function that takes multiple iterables as input and returns them as a single iterable. Instead of creating a new list containing all the elements up front, it generates the elements on demand, which can be more memory efficient.Using itertools.chain()
itertools.chain() to flatten a list of lists, you need to import the itertools module and use the chain.from_iterable() method. Here’s how to proceed:chain.from_iterable() takes a single iterable of iterables (i.e., a list of lists) and transforms it into a flat iterable.Advantages of itertools.chain()
- Memory efficiency: Since
chain()generates elements on demand, it can be more memory-efficient than first building a large list. - Performance: This method is well-optimized for chaining iterables, which can make it faster than manual methods, especially for large lists.
- Simplicity: Once you understand how it works,
itertools.chain()is simple to use and reduces the need to write complex loops.
Comparison with other methods
| Method | Memory efficiency | Performance | Simplicity |
|---|---|---|---|
| List comprehension | Medium | Good | Medium |
for loop | Low | Average | High |
itertools.chain() | High | Very good | Medium |
Example with complex iterables
itertools.chain() can also be used to flatten more complex iterables, like generators or iterators.generators() is a generator function that produces lists, and itertools.chain() flattens them efficiently.Use cases
itertools.chain() is particularly advantageous when:- You are working with streaming data or iterables that do not fit in memory.
- You are looking to maximize memory efficiency with large data collections.
- You need an elegant and performant solution without writing a lot of code.
itertools.chain() is a robust and efficient solution for flattening lists in Python, particularly suited for situations requiring careful memory management and optimal performance.Concatenating lists with functools.reduce()
functools.reduce() function is a powerful method for applying a function cumulatively to a sequence of elements to reduce them to a single value. In the context of flattening lists of lists, it can be used to concatenate the sub-lists into a single list.Understanding functools.reduce()
functools.reduce() is part of the standard Python library and serves to apply a binary function repeatedly on the elements of an iterable. The first argument is the function to apply, the second is the iterable, and the third (optional) is an initial value.Using reduce() to flatten a list of lists
reduce(), we use the operator.add function to concatenate the sub-lists. Here’s how it works:operator.add is used to concatenate each sub-list to a cumulative list, initialized as an empty list [].Advantages of functools.reduce()
- Expressiveness:
reduce()allows condensing multiple concatenation operations into a single line, making the code more concise. - Flexibility: It is possible to replace
operator.addwith other binary functions for more complex or custom operations.
Disadvantages of functools.reduce()
- Readability: For those not familiar with
reduce(), the code may seem obscure, as the reduction logic is not as explicit as in loops or list comprehensions. - Performance: Using
reduce()withoperator.addis not always the most performant approach, as it can lead to repeated copies of lists in memory, especially for very large lists.
Comparison with other methods
| Method | Conciseness | Performance | Readability |
|---|---|---|---|
| List comprehension | High | Good | Medium |
for loop | Medium | Average | High |
itertools.chain() | Medium | Very good | Medium |
functools.reduce() | High | Medium to low | Low |
Example of use with a custom function
reduce() with a custom function to perform additional operations during flattening:add_if_greater is a function that concatenates lists while filtering elements greater than a given threshold. This shows how reduce() can be employed for more complex tasks.Conclusion
functools.reduce() is a viable option for flattening lists, it is often less performant and less readable compared to other methods like itertools.chain(). However, it remains useful in specific situations where additional flexibility or complex logic is required.Using sum() to concatenate lists
sum() function to concatenate lists is a simple and intuitive approach to flattening a list of lists in Python. Although this method is easy to understand, it has some peculiarities and limitations that we will explore.How sum() works with lists
sum() is typically used to add numbers. However, it can also be used to concatenate lists by specifying an empty list as the starting point. Here’s the basic syntax for flattening a list of lists with sum():sum() starts with an empty list [] and adds each sub-list to this list.Advantages of using sum()
- Simplicity: The method is very simple to read and write, making it accessible even for beginners.
- Integration: Since
sum()is a built-in function, it does not require any additional imports, which reduces code complexity.
Disadvantages of using sum()
- Performance: Using
sum()to concatenate lists can be inefficient for very large lists. With each addition, a new list is created, which can lead to high memory consumption and increased slowness. - Misuse: Although possible, this is not the recommended method for concatenating lists, as it is not optimized for this task.
Comparison with other methods
| Method | Simplicity | Performance | Recommended use |
|---|---|---|---|
| List comprehension | Medium | Good | Yes |
for loop | Medium | Average | Yes |
itertools.chain() | Medium | Very good | Yes |
functools.reduce() | Medium | Medium to low | Yes, depending on the context |
sum() | Very high | Low | Not recommended for large lists |
Use cases
sum() may be acceptable in situations where:- The list of lists is relatively small, and performance is not a critical criterion.
- You want quick-to-write and easy-to-understand code for scripts or one-off tasks.
Example with small lists of lists
sum() is used to flatten a relatively small list of lists:Conclusion
sum() to concatenate lists is a straightforward and easy-to-understand method. However, due to its inefficiency for large data structures, it is generally not recommended for data-intensive applications. For better performance and efficient memory management, alternatives like itertools.chain() or list comprehensions are preferable.Considering performance when flattening your lists
Importance of performance
Comparison of methods in terms of performance
- Loop :
- Advantages: Easy to understand and implement.
- Disadvantages: Can be slow for large lists due to explicit iteration over each element.
- List comprehension:
- Advantages: Generally faster than traditional
forloops, as it is optimized internally by Python. - Disadvantages: Can become complex to read for very nested operations.
- Advantages: Generally faster than traditional
itertools.chain():- Advantages: Very efficient for flattening lists, uses a generator to create elements on demand, saving memory.
- Disadvantages: Requires importing an external module, which can be a barrier for beginners.
functools.reduce():- Advantages: Allows complex operations during flattening.
- Disadvantages: Less performant for simple concatenations, as it involves repeated copies of lists.
sum():- Advantages: Very simple to implement.
- Disadvantages: Inefficient for large lists due to the creation of new lists at each summation step.
Choosing the right method
- Small lists: For smaller data sets, the simplicity of
sum()orforloops may be sufficient, as the performance impact is minimal. - Large lists: For large data volumes, prefer
itertools.chain()or list comprehensions to optimize speed and memory usage.
Measuring and optimizing performance
timeit, which can help you evaluate the execution time of different approaches.Conclusion
Flattening Python lists for data science with NumPy
Why use NumPy?
ndarray, which allow performing complex mathematical operations quickly and efficiently. Using NumPy to flatten lists offers several advantages:- Performance: NumPy is optimized for vectorized operations, making it much faster than standard Python lists for operations on large amounts of data.
- Simplicity: With NumPy, operations on arrays are often simpler and more readable.
- Advanced features: NumPy has built-in methods for flattening, transforming, and manipulating arrays concisely.
Using numpy.flatten()
flatten() method in NumPy is used to convert a multi-dimensional array into a one-dimensional array. Here’s an example of its use:flatten() transforms the 2D array into a 1D array while preserving the order of the elements.Using numpy.ravel()
ravel(). It works similarly to flatten(), but it is often more efficient because it returns a view of the original array when possible, rather than making a copy.ravel() is recommended when you want to minimize memory use, as it does not necessarily create a new copy of the data.Method comparison
| Method | Copy/View | Performance | Memory Use |
|---|---|---|---|
flatten() | Copy | Good | Higher |
ravel() | View (if possible) | Very good | Lower |
Considerations for data science
- You are working with feature matrices or multi-dimensional data samples.
- You need to prepare data for machine learning algorithms that require one-dimensional inputs.
- You are manipulating large amounts of data where efficiency is crucial.
flatten() and ravel(), NumPy makes working with multi-dimensional data easier, providing fast and pragmatic solutions for flattening lists and arrays.Conclusion
Summary of methods
- Loops: This approach is intuitive and easy to understand, ideal for beginners. However, it can become inefficient for large lists due to increased time complexity.
- List comprehensions: Offer concise syntax and are generally more performant than traditional loops. They are well-suited for developers looking to write code that is both compact and clear.
itertools.chain(): A performant solution that uses a generator to save memory. It is particularly useful for processing large lists without overloading memory.functools.reduce(): While flexible and expressive, this method can be inefficient for simple concatenations due to repeated list copies.sum(): Simple to implement, but strongly discouraged for large lists due to performance issues.- NumPy: For data science users, NumPy offers optimized methods like
flatten()andravel(), ideal for efficiently manipulating multi-dimensional arrays.
Factors to consider
- Performance: For large lists, prefer
itertools.chain()and NumPy to optimize speed and minimize memory usage. - Readability and simplicity:
forloops and list comprehensions offer good readability. However, for experienced developers,itertools.chain()can also be easily understandable. - Application context: In data science, where performance and manipulation of multi-dimensional arrays are essential, NumPy is often the best choice.
Choosing the appropriate method
- Beginners or small lists: A
forloop or a list comprehension may suffice. - Large lists or critical performance: Use
itertools.chain()or NumPy. - Complex or custom operations:
functools.reduce()can be useful, but ensure you understand its implications in terms of performance.
Best practices
- Testing and measuring: Before choosing a method, use tools like
timeitto test the performance of your code in realistic scenarios. - Optimization: Always optimize based on the intended use. Simplicity is not always synonymous with efficiency, especially with large data sets.
Frequently asked questions
What is the fastest method for flattening a list of lists in Python?
itertools.chain() is often the most efficient method, as it uses a generator to create elements on demand, thus saving memory. For multi-dimensional arrays in the context of data science, NumPy, with its ravel() method, offers excellent performance due to its optimization for numerical computing.Why is sum() not recommended for flattening large lists?
sum() is generally not recommended for flattening large lists because it creates a new list at each iteration by adding elements, which can lead to excessive memory consumption and significant slowdowns. This method is simple and intuitive, but it is not optimized for handling large data sets.When should I use functools.reduce() to flatten lists?
functools.reduce() is useful when you need to apply a complex function cumulatively over the elements of a list. However, for simple flattening, it is generally less performant than other methods like itertools.chain() or list comprehensions. Use it if you have additional operations to perform on the elements during flattening.Are list comprehensions always a good option?
itertools.chain() or NumPy might offer better performance.How to choose between flatten() and ravel() in NumPy?
flatten() and ravel() lies in their memory management. flatten() returns a copy of the array, while ravel() returns a view of the original array when possible, which can save memory. If you do not need to modify the elements of the flattened array, ravel() is often the better choice due to its efficiency.Is it possible to flatten nested lists recursively?
What method is the most readable for beginners?
for loop is often the most readable and easy to understand. Although less concise than list comprehensions, it allows for a clear visualization of the iteration and manipulation process, which is educational and formative for acquiring a basic understanding of list manipulation in Python.Want to go further?
This topic is part of our Become a Data Analyst course. Browse the full programme, or get it by email.
Share with

Romain DE LA SOUCHÈRE
Tech Lead, CTO AXI Technologies
Expert Data Engineering et Cloud, Romain affiche plus de 11 ans d'expérience, dont plusieurs années comme Lead Developer sur des solutions Smart Building haute performance. Il y a conçu et mis en production des moteurs de traitement capables d'absorber des centaines de milliers de données de capteurs par minute, ainsi que des bases clusterisées gérant plus de 10 millions de données dynamiques. Certifié Microsoft Azure DevOps Engineer Expert, il maîtrise aussi bien le développement back-end (Python, C#) que le DevOps (Docker, Kubernetes, Terraform) et les agents LLM. Formateur en Python, cloud, DevOps et IA générative appliquée, il forme avec une obsession : Amener chaque apprenant à concevoir et déployer des architectures réellement scalables en production.
» Learn MoreAssociated trainings
All our trainings →
Associated articles
See all our articles →
janvier 2, 2025
Measuring Execution Time with time and datetime - Practical Tutorial in PythonReading time: 8 min




