Access to the list of tuples

I have a list containing several tuples, for example:

[('a_key', 'a value'), ('another_key', 'another value')]

where the first values ​​of the tuple act as dictionary keys. Now I'm looking for a python-like way to access key / value pairs, for example:

"mylist.a_key" or "mylist['a_key']"

without repeating the list. any ideas?

+3
source share
2 answers

- . , dict, , . - , , , - .

>>> x = [('a_key', 'a value'), ('another_key', 'another value')]
>>> y = dict(x)
>>> y['a_key']
'a value'
>>> y['another_key']
'another value'
+14

, ( , ).

, Van Gale defaultdict - , .

Edit:

, defaultdict , , . , "" , .

+3

Source: https://habr.com/ru/post/1706169/


All Articles