Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a dict into a list

Tags:

python

I have

{key1:value1, key2:value2, etc}

I want it to become:

[key1,value1,key2,value2] , if certain keys match certain criteria.

How can i do it as pythonically as possible?

Thanks!

like image 555
user1008636 Avatar asked Nov 29 '22 02:11

user1008636


1 Answers

This should do the trick:

[y for x in dict.items() for y in x]

For example:

dict = {'one': 1, 'two': 2}

print([y for x in dict.items() for y in x])

This will print:

['two', 2, 'one', 1]
like image 132
Ry- Avatar answered Dec 07 '22 23:12

Ry-