Search code examples
pythonlistunpack

Python: how to add remaining list elements to list, similar to unpack?


I have a list, which can have different size but it always have 1st item. I writing such comprehension:

return {
    'index': [[i[0][7:], i[1], i[2], i[3]] for i in columns if i[0].startswith("index::")]
}

And part i[1], i[2], i[3] could vary. It can have 0 or more size and I need to specify them as list elements.

Something like

return {
    'index': [[i[0][7:], *i[1:]] for i in columns if i[0].startswith("index::")]
}

would be great.

Input:

[['index::test', '1', '2', '3']]
[['index::test', '1', '2', '3', '5']]
[['index::test']]

Output:

[['index', '1', '2', '3']]
[['index', '1', '2', '3', '5']]
[['index']]

Solution

  • If I understand you right, you want to use [i[0][7:]] + i[1:].