Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dict.setdefault(key, []).append() --> get rid of additional list

How can i prevent from adding a list into a list when using setdefault with a list type definition.

output = dict()
output.setdefault("key", []).append(["name", 1])
print output
>>> {'key': [['name', 1]]}

Desired output

>>> {'key': ['name', 1]}
like image 488
user1767754 Avatar asked Jan 08 '23 03:01

user1767754


1 Answers

You want .extend rather than .append - the former adds a list of items to a list, the latter adds a single item - so if you pass it a list, it adds the list as a single subitem.

like image 132
Amber Avatar answered Jan 10 '23 16:01

Amber