Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

flatten list python

def flatten(L):
    for item in L:
        try:
            yield from flatten(item)
        except TypeError:
            yield item

list(flatten([[[1, 2, 3], [4, 5]], 6]))
>>>[1, 2, 3, 4, 5, 6]
Comment

flatten list python

from typing import Iterable

# flatten any number of nested iterables (lists, tuples)
def flatten_iterables(iterable: Iterable) -> list:
    """Convert iterables (lists, tuples) to list (excluding string and dictionary)

    Args:
        iterables (Iterable): Iterables to flatten

    Returns:
        list: return a flattened list
    """
    lis = []
    for i in iterable:
        if isinstance(i, Iterable) and not isinstance(i, str):
            lis.extend(flatten_iterables(i))
        else:
            lis.append(i)
    return lis
Comment

.flatten() python

>>> a = np.array([[1,2], [3,4]])
>>> a.flatten()
array([1, 2, 3, 4])
>>> a.flatten('F')
array([1, 3, 2, 4])
Comment

PREVIOUS NEXT
Code Example
Python :: py scrapy 
Python :: python editor online 
Python :: python get an online file 
Python :: django add user to group 
Python :: How to use path in Django Python 
Python :: python tuple methods 
Python :: numpy and operator 
Python :: dataframe-name python 
Python :: list python 
Python :: how to find the average in python 
Python :: Python - How To Convert Bytearray to String 
Python :: how to make python print 2 line text in one code 
Python :: what is repr function in python 
Python :: last element python 
Python :: data type 
Python :: combination in python math 
Python :: while loop in python for do you want to continue 
Python :: how to create models in django 
Python :: python if syntax 
Python :: pyqt5 buttons 
Python :: how to make a calculator in python 
Python :: indent python 
Python :: python scatter size 
Python :: add Elements to Python list Using insert() method 
Python :: how to store data in python 
Python :: pytorch get tensor dimension 
Python :: python list to sublists 
Python :: python curses resize window 
Python :: AttributeError: __enter__ in python cde 
Python :: python3 vowels and consonants filter 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =