Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Python dictionary question

I have a dictionary with one key and two values and I want to set each value to a separate variable.

d= {'key' : ('value1, value2'), 'key2' : ('value3, value4'), 'key3' : ('value5, value6')}

I tried d[key][0] in the hope it would return "value1" but instead it return "v"

Any suggestions?

like image 961
joepour Avatar asked Feb 07 '26 04:02

joepour


2 Answers

A better solution is to store your value as a two-tuple:

d = {'key' : ('value1', 'value2')}

That way you don't have to split every time you want to access the values.

like image 167
zenazn Avatar answered Feb 09 '26 10:02

zenazn


Try something like this:

d = {'key' : 'value1, value2'}

list = d['key'].split(', ')

list[0] will be "value1" and list[1] will be "value2".

like image 26
Andrew Hare Avatar answered Feb 09 '26 10:02

Andrew Hare