given a list of list return the list of integer values from the list

accessing list of lists python
python assign list values to variables
python list of lists
how to store values in list in python
check the attributes of a collection for which using a python list would be appropriate
python convert list elements to int
how to create nested list in python
convert single int to list python

given a list

lt=[1,2,[3,[5,[6,[7]]]]] 

I know this is a basic question and I have done it with for loops but, I need to understand how this basic question can be done in a single or two lines. This was asked to me in an interview.

return [1,2,3,5,6,7]

Use a function to flatten recursively.

Solution
# Custom function
def get_flatlist(l, flat_list=None, no_more_list_elements = True):
    if flat_list is None:
        flat_list = list()

    for x in l:
        if isinstance(x,list):        
            flat_list += x
            no_more_list_elements = no_more_list_elements & False
        else:
            flat_list.append(x)
            no_more_list_elements = no_more_list_elements & True
    #print(no_more_list_elements)
    if not no_more_list_elements:        
        flat_list = get_flatlist(flat_list.copy(), 
                                 flat_list=None, 
                                 no_more_list_elements = True)
    return flat_list    

get_flatlist(lt)

Output:

[1, 2, 3, 5, 6, 7]
Data
lt=[1,2,[3,[5,[6,[7]]]]] 

Lists, The values in list are called elements or sometimes items. The following list contains a string, a float, an integer, and (lo!) another list: This loop traverses the list and updates each element. len returns the number of elements in the list. range returns a list of Similarly, the * operator repeats a list a given number of times: A naive method to create list within a given range is to first create an empty list and append successor of each integer in every iteration of for loop. filter_none edit

This is a modified version of @DarrylG's solution, helping to get the line count down.

flatten = lambda lst: sum((flatten(v) if isinstance(v, list) else [v] for v in lst), [])
flatten(lt)

Lambda functions can be recursive, but are one line shorter then function declarations.

And this could also be in one line by using a ;

flatten = lambda lst: sum((flatten(v) if isinstance(v, list) else [v] for v in lst), []) ; flatten(lt) 

Python, map function can be used to perform the following task converting each of the string converted number to the desired integer value to be reconverted to the list​  Given a list of integers with duplicate elements in it. The task to generate another list, which contains only the duplicate elements. In simple words, the new list should contain the elements which appear more than one.

Method to flatten lists of lists (1-2 line function) (mod of Flatten an irregular list of lists)

  1. Use a generator to create single element lists
  2. Use sum to append these into a single list

def flatten(lst):
    return sum( ([x] if not isinstance(x, list) else flatten(x)
                 for x in lst), [] )

lt=[1,2,[3,[5,[6,[7]]]]]

print(flatten(lt))

Output

[1, 2, 3, 5, 6, 7]

List get() method in Java with Examples, E get(int index) Where, E is the type of element maintained by this List container. Return Value: It returns the element at the specified index in the given list. Given a list of numbers, the task is to write a Python program to find the smallest number in given list. Examples: Input : list1 = [10, 20, 4] Output : 4 Input : list2 = [20, 10, 20, 1, 100] Output : 1. Method 1 : Sort the list in ascending order and print the first element in the list.

11. Lists, The values that make up a list are called its elements, or its items. We will use the The first example is a list of four integers. The second is a List length¶. The function len returns the length of a list, which is equal to the number of its elements. Similarly, the * operator repeats a list a given number of times: >>> [0​] * 4 [0  Convert List of String to List of Integer using Lists.transform (). This is done using passing Integer.parseInt () method as lambda expression for transformation. Return/Print the list of String.

9. Lists, numbers[-1] is the last element of the list, numbers[-2] is the second to last, and The function len returns the length of a list, which is equal to the number of its elements. It is a Similarly, the * operator repeats a list a given number of times:. The original list is : [1, None, 4, None, None, 5, 8, None] List after removal of None values : [1, 4, 5, 8] Method #2 : Using list comprehension The longer task of using the naive method and increasing line of codes can be done in a compact way using this method.

Python lists - working with lists in Python, It contains numbers, a boolean value, another list, a string, a tuple, a custom The max() and min() functions return the maximum and the minimum of the list. The insert() method places an element at a specific position  Hi the code below shown this method from another class in C#. May I know how do I return a List<string> to the other class so that the order class could access the data

Comments
  • Possible duplicate of Flatten an irregular list of lists
  • are you sure it's only two lines? With recursion you might do something with two lines, but then you had to call the declared function, which would be a third line. You see for example an answer from DarryG, but ut us using three lines and not two. two lines do ceclare the function and one to call it.
  • @bencv: Yes in fact this is a duplicate. One of the solutions in stackoverflow.com/questions/2158395/… uses only two lines (or one with ';') It's Statham's answer. However reduce is no more a predeclared function in python3, so one had to add some imports to the line