Expand a inner list into a flat list

Question:

I have a list of list (var_list) that I want to "expand" into a flat list (list1), see bellow:

environment = {
    "KEY1": "VALUE1",
    "KEY2": "VALUE2",
}

var_list = [
    ["var", f"{key}={value}"] for key, value in environment.items()
]

list1 = ["foo", "bar"]

result = list1.extend(var_list)

# PS. Must be a list, not a `' '.join`

print(f"{result=}") # None.

print(result == ["foo", "bar", "var", "KEY1=KEY1", "var", "KEY2=KEY2"]) # returns False. Should be True or equal

How can I do this in python, without using ' '.join?

Asked By: Rodrigo

||

Answers:

Found the solution, I can use operator.concat

list1 = ["foo", "bar"] + functools.reduce(operator.concat, var_list)
Answered By: Rodrigo
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.