Home > Net >  Ideas for the following function?
Ideas for the following function?

Time:11-06

I would like to develop a function which would always based on the first element in the list extract further elements of the nested list. Example: Input: lst= [[1,2,3],[3,4,5,6,8,9], [0,3,4], [1,4,5,6], [4,9,8,6,5,2]] The first element in the sublist always determines the number of following elements to append to a new list. Output: Out=[2,4,5,6,4,9,8,6,5] Elements are always an integer values.

CodePudding user response:

Maybe this is what you expect:

Or you can convert this to List Comprehension later.

out = []

for sub in lst:                 # loop each sublist
    start = sub[0]              # use the first num. to determine
    out.extend(sub[1: 1 start]) # get each sublist by slicing

print(out)

[2, 4, 5, 6, 4, 9, 8, 6, 5]
  • Related