3 Ways To Flatten Any List In Python

  Рет қаралды 5,481

Indently

Indently

Күн бұрын

Learn how you can flatten any list in Python. We will be exploring 3 different ways to do this. First will be through vanilla Python with the for loop, then we will use the faster list comprehension approach, and finally we will use the easiest approach with numpy.

Пікірлер: 15
@hydra147147
@hydra147147 2 жыл бұрын
One can also try this implementation: def flatten_any(input_list: list) -> list: return sum(map(flatten_any, input_list), []) if isinstance(input_list, list) else [input_list]
@Indently
@Indently 2 жыл бұрын
Works great! Although it's incredibly difficult to read, but that can be really hard to avoid if you want short code to flatten lists.
@jbn999
@jbn999 Жыл бұрын
FYI - This was my interview question with Facebook. - Recursion would make anything flat whatever your throw at it.
@wartem
@wartem Жыл бұрын
Well, you have to handle call stack issues when throwing "whatever" at it. It has limits and recursion isn't recommended in production code.
@alexisdamnit9012
@alexisdamnit9012 10 ай бұрын
I was surprised this video didn't have a recursion solution. smh
@ezrawick
@ezrawick Жыл бұрын
Great! I was looking for this solution last week.
@treadaar
@treadaar Жыл бұрын
lst = [[[[1,2],[3,4,['some', 'text']]],[[5,6, [0, ['l', 'j']]],[7,8]]],[9]] print(lst) # recursively flatten list using yield from to generate a new list def flatten_list(lst): for item in lst: if isinstance(item, list): yield from flatten_list(item) else: yield item print(list(flatten_list(lst))) I would use this, much easier and flattens any number of nested lists with any content.
@padraic1983
@padraic1983 Жыл бұрын
Just turn the array into a string , use list comp to filter
@padraic1983
@padraic1983 Жыл бұрын
Lol of course that's just being cheap ;)
@wartem
@wartem Жыл бұрын
Or regex
@padraic1983
@padraic1983 Жыл бұрын
@@wartem that's why I said I was being cheap
@j.raimundosilva2101
@j.raimundosilva2101 Жыл бұрын
I have a clean pythonic way to do this, but if the first index of the deepest item is list, set ou tuple, it ends the flatten function: def flatten(input_list) -> list: flatten_layer = [item for subitem in input_list for item in subitem] if not isinstance(flatten_layer[0], (list, tuple, set)): return flatten_layer return flatten(flatten_layer) if you copy this function you will see that it flattens any dimension of list given its other limits.
@ZeroSleap
@ZeroSleap Жыл бұрын
So basically at first you just showed two functions to flatten them yourself and at the end you succumbed to using a superior implementation from numpy.Ok i guess. I was expecting a recursive approach,but alright.
@Indently
@Indently Жыл бұрын
I will post an updated version with recursive approach.
@sammflynn405
@sammflynn405 Жыл бұрын
what's wrong with flatten = list(itertools.chain(*list_any))
Try.. & Except Has Some Secret Functionality in Python
2:27
10 Nooby Mistakes Devs Often Make In Python
24:31
Indently
Рет қаралды 64 М.
This mother's baby is too unreliable.
00:13
FUNNY XIAOTING 666
Рет қаралды 38 МЛН
哈哈大家为了进去也是想尽办法!#火影忍者 #佐助 #家庭
00:33
火影忍者一家
Рет қаралды 126 МЛН
龟兔赛跑:好可爱的小乌龟#short #angel #clown
01:00
Super Beauty team
Рет қаралды 14 МЛН
How To Write Better Functions In Python
14:17
Indently
Рет қаралды 37 М.
Flattening a JSON Object Using Recursion in Python
23:30
Coderbyte
Рет қаралды 12 М.
Enums Explained In Under 12 Minutes In Python
12:03
Indently
Рет қаралды 26 М.
Python Code Challenge - Flatten Nested List
19:07
Very Academy
Рет қаралды 1,5 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50
10 Crazy Python Operators That I Rarely Use
11:37
Indently
Рет қаралды 37 М.
Learn Python OOP in under 20 Minutes
18:32
Indently
Рет қаралды 63 М.
Flatten Nested List Iterator - Leetcode 341 - Python
10:14
NeetCodeIO
Рет қаралды 10 М.
5 Good Python Habits
17:35
Indently
Рет қаралды 565 М.
This mother's baby is too unreliable.
00:13
FUNNY XIAOTING 666
Рет қаралды 38 МЛН