Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to assign multiple variable value from key value of dict in python?

Tags:

python

here HEX2RGB return dict value after conversion like {r:255,g:255,b:255}

    rgbValue=HEX2RGB(hex,'dict')
    r,g,b={rgbValue} # not working

so how can I assign value of each key r,g,b in the variables r,g,b like above method? or is there any other efficient way?

like image 519
Experi Ment Avatar asked May 31 '26 22:05

Experi Ment


2 Answers

Use operator.itemgetter, which will not rely on any particular ordering returned by HEX2RGB;

from operator import itemgetter

dictToTuple = itemgetter('r', 'g', 'b')

r, g, b = dictToTuple(HEX2RGB(hex,'dict'))
like image 119
chepner Avatar answered Jun 02 '26 10:06

chepner


if your type is a dictionary (assumption cause you wrote {r:255,g:255,b:255})

r,g,b = rgbValue.get('r'),rgbValue.get('g'),rgbValue.get('b')

More Pythonic way:

dict = {'r':255,'g':255,'b':255}
r,g,b = dict.values()

print(r)
print(g)
print(b)
like image 37
Roman Avatar answered Jun 02 '26 12:06

Roman