-4

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?

Rodrigo
  • 135
  • 4
  • 45
  • 107

1 Answers1

0

Found the solution, I can use operator.concat

list1 = ["foo", "bar"] + functools.reduce(operator.concat, var_list)
Rodrigo
  • 135
  • 4
  • 45
  • 107
  • Surely now `print(f"{result=}") ` will fail. – quamrana Apr 20 '21 at 13:53
  • There's an error is line `result = list1.extend(var_list)`, because the extend method changes the `list1` itself and returns None. So, you don't need a `result` list. – wensiso Apr 20 '21 at 14:03