in Education by
Is there any shortcut to make a simple list out of a complex one in python? I already performed for a loop but searching for a one liner, when I try to reduce it, I got an error, given below for Ref. Syntax: l = [[2, 3, 4], [5, 6, 7], [8], [9, 0]] reduce(lambda z, x: z.extend(x), l) Error: Traceback (most recent call last): File "", line 1, in File "", line 1, in AttributeError: 'NoneType' object has no attribute 'extend' Please help me with it. Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Here's what you can do (this applies to strings, numbers, mixed containers and nested lists) from collections import Iterable def flatten(items): """Yield items from any nested iterable; see Reference.""" for f in items: if isinstance(f, Iterable) and not isinstance(f, (str, bytes)):for sub_f in flatten(f):yield sub_f else:yield f If you are using Python 3 then, replace for sub_f in flatten(f): yield sub_f with yield from flatten(f) Ex. lst = [[2, 3, 4], [5, 6, 7], [8], [9, 0]] list(flatten(lst)) # nested lists # [2, 3, 4, 5, 6, 7, 8, 9, 0] You can also use timeit module from standard library: $ python -mtimeit -s'm=[[2,3,4],[5,6,7], [8], [9,0]*99' '[item for sublist in p for item in sublist]' 10000 loops, best of 3: 254 usec per loop $ python -mtimeit -s'l=[[2,3,4],[5,6,7], [8], [9,0]]*99' 'sum(p, [])' 1000 loops, best of 3: 070 usec per loop $ python -mtimeit -s'p=[[2,3,4],[5,6,7], [8], [9,0]]*99' 'reduce(lambda q,w: q+w,p)' 1000 loops, best of 3: 1.1 msec per loop Happy Learning.

Related questions

0 votes
    Is there a built-in that removes duplicates from a list in Python, whilst preserving order? I know that I can ... idiom if possible. Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    suppose I have passed : list=[] How can I check if a list is empty or not? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How to remove an element from a list by index in Python, I tried list.remove method but it search the list and ... How can I do this? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have an item and I want to count it's occurrence in a list, How can I do that in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have a list: foo= ['q'' , 'w' , 'e' , 'r' , 't' , 'y'] I want to retrieve an item randomly from this list, How can I do it in python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I am confused about the usage of string.join(list) instead of list.join(string), for instance see this code: ... reason behind this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can someone get a index (1) in Python for a list ["qwe", "asd", "zxc"] and an item "asd" in the list? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I think the slice notation are sort of powerful but I don't know much about it, So can anyone give me a ... Python's slice notation? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact ... *", i) in a] Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    Imagine that you have: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] What is the simplest way ... 42, 'food' : 'spam'} Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How can I do the following in Python? array = [0, 10, 20, 40] for (i = array.length() - 1; i >= ... but from the end to the beginning. Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    In Python 2.7, I could get dictionary keys, values, or items as a list: >>> newdict = {1:0, 2:0, 3:0} ... to return a list in Python 3? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    Can someone tell me any methods to read every line of a file in Python and store each line as an element in a ... till the end of list. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I want each item to be sorted by a specific property value from a list of dictionaries I have. Take the ... When sorted by name Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
...