If you’re curious about this approach, I discuss it in more detail in another post. The following code snippet illustrates that. The second strategy is a bit unconventional and, truth to be told, very "magical".ĭid you know that we can use the built-in function sum to create flattened lists?Īll we need to do is to pass the list as an argument along an empty list. > lst =, 3, ( 4, 5),, "hello", 3, 4, "hello"]įlattening a list of lists with the sum function The only downside is that if the list is big, there'll be a performance penalty since we need to create the set using the generator, then convert set to list. To flatten a list of lists and return a list without duplicates, the best way is to convert the final output to a set. How to flatten a list and remove duplicates Lastly, this flatten function works for multidimensional list of mixed types. Since we check if sublist is a list of not, this works with list of tuples as well, any list of iterables, for that matter. > def flatten( lst: List) -> Iterable: """Flatten a list using generators comprehensions. One way of preventing that is by checking if the item is a list of not. Oops, that's not what we want! The reason is, since one of the items is iterable, the function will unfold them as well. Let's see what happens if we plug a list of lists and strings. Otherwise, it flattens them as well, which is the case for strings. The technique we've seen assumes the items are not iterables. How to flatten list of strings, tuples or mixed types > flatten_lambda = lambda lst: (item for sublist in lst for item in sublist) If you prefer you can make the code shorter by using a lambda function. When we run the test we can see it passing. This function returns a generator, to get a list back we need to convert the generator to list. from typing import List, Any, Iterableĭef flatten_gen_comp( lst: List) -> Iterable: """Flatten a list using generators comprehensions.""" return (itemĪssert list(flatten_gen_comp(lst)) = To make sure everything works as expected, we can assert the behavior with the test_flatten unit test. We can then use the generators to create a single list. The reason for that is to avoid building a whole list in memory. The following function accepts any multidimensional lists as an argument and returns a generator. We iterate through each sublist, then iterate over each one of them producing a single element each time. The first way of doing that is through list/generator comprehensions. In other words, we want to convert the original list into a flat list like this. Let’s imagine that we have a simple list of lists like this, , ] and we want to flatten it. Which method is faster? A performance comparisonįlattening a list of lists with list comprehensions Flatten a regular list of lists with numpy.Flattening a nested list of lists with the sum function.(#how to flatten a list and remove duplicates).How to flatten list of strings, tuples or mixed types.Flattening a list of lists with list comprehensions.the best way to flatten a nested list using recursion or without recursion.how to use itertools chain to create a flat list.how to use numpy to flatten nested lists.how to convert a nested list of lists using the built-in function sum from the standard library. how to unfold a list and remove duplicates.the best way to flatten lists of lists with list comprehensions.how to flatten / unnest a list of mixed types, including list of strings, list of tuples or ints.By going over each one, you’ll learn how to identify the most appropriate solution for your problem by creating your own flatten() function in Python.įor all examples, we'll use Python 3, and for the tests pytest.īy the end of this guide, you'll have learned: Each method has pros and cons, and varies in performance. In this post, we’ll see how we can unnest an arbitrarily nested list of lists in 7 different ways. What about lists with mixed types such as list of strings, or list of tuples? In another words, how to turn 2-D lists into 1D: Ever wondered how can you flatten, or unnest, a 2D list of lists in Python?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |