Hillary Clinton, s
Surgical treatment
Q:
Can I use a C#
Dennis Wilson is a
/*
* This file is
Introduction
=====
Category: Tile Roo
Krampus: Reindeer
Tesla founder Elon
A couple years aQ:
Python: Create a new key value pair within a list
I have a dictionary with the following values.
{'a': [100, 1],
'b': [200, 2],
'c': [300, 3]}
How can I create a new key value pair within the dictionary so that I get the following:
{'a': [100, 1],
'a1': [101, 1],
'a2': [102, 1],
'b': [200, 2],
'b1': [201, 2],
'b2': [202, 2],
'c': [300, 3],
'c1': [301, 3],
'c2': [302, 3]}
I know how to add a new key to an already defined key in a dictionary with the following:
d = {k: [v]+[k1] for k,v in d.items() for k1 in range(k)}
What I would like to do is to create a new key within a list that's inside a dictionary and have a dictionary with an array of arrays with 2 indexes. Is this possible in python?
A:
If you need to change something existing in a dict, it should be modified like this:
{'a': [100, 1], 'a1': [101, 1], 'a2': [102, 1]}
With dict you cannot have keys that don't exist yet, or you would get an exception. This is the reason you get the error that index is out of range when trying to access a1.
That said, dict objects support the addition of keys with dict.update(some_dict, key, value), and you can use it to set items in the right position:
mydict = {'a': [100, 1], 'b': [200, 2], 'c': [300, 3]}
dict_copy = {k: list(v) for k,v in mydict.items()}
for i in range(len(mydict['a'])):
dict_copy['a'[i]].append(dict_copy['a'[i] + 1])
dict_copy['a'[0]].append(101)
dict_copy['a'[1]].append(201)
dict_copy will now be:
{'a': [100, 1, 101, 1], 'b': [200, 2], 'c': [300, 3]}
Now with the new dict_copy you can do:
for k, v in dict_copy.items():
v.append('A1')
v.append('A2')
which will give you:
{'a': [100, 1, 101, 1, 'A1'], 'a': [100, 1, 101, 1, 'A2'],
'b': [200, 2], 'c': [300, 3]}
I would highly recommend using a collections.namedtuple for your use case:
from collections import namedtuple
from collections import OrderedDict
MyNamedTuple = namedtuple('MyNamedTuple', ['a', 'a1', 'a2'])
mydict = {'a': [100, 1], 'b': [200, 2], 'c': [300, 3]}
dict_copy = OrderedDict()
for key, value in mydict.items():
d = MyNamedTuple(*value)
dict_copy[key] = list(d)
dict_copy['a1'].append('A1')
dict_copy['a2'].append('A2')
dict_copy will be:
OrderedDict([('a', MyNamedTuple([100, 1], ['A1', 'A2'])),
('b', MyNamedTuple([200, 2])),
('c', MyNamedTuple([300, 3])),
('a1', ['A1', 'A1']),
('a2', ['A2', 'A2'])])
This is an interesting example of the use of collections.namedtuple:
# with collections.namedtuple
Person = namedtuple('Person', ['name', 'surname', 'age'])
>>> l = [('m1', {'surname': 'Foo', 'age': 20, 'name': 'John'})]
>>> P = Person(*m)
>>> m.name
'John'
>>> P.name
'John'
>>>
A:
How about:
mydict = {'a': [100, 1], 'b': [200, 2], 'c': [300, 3]}
dict_copy = {}
for key, value in mydict.items():
if key not in dict_copy:
dict_copy[key] = [value[0]]
else:
dict_copy[key].append(value[1])
print dict_copy
{'a': [100, 1], 'a1': [101, 1], 'b': [200, 2], 'c': [300, 3]}
You might want to have a more robust version of the above (checking for duplicates for instance) but this should work for your example.
A:
You can use an OrderedDict to do that:
In [35]: from collections import OrderedDict
In [36]: d = {'a': [100, 1], 'b': [200, 2], 'c': [300, 3]}
In [37]: dict_copy = OrderedDict()
In [38]: for k in d:
....: dict_copy[k] = [d[k[0]]]
....: dict_copy[k + "1"] = [d[k[1]] + 1]
....:
In [39]: dict_copy
Out[39]:
OrderedDict([('a', [100, 1]),
('b', [200, 2]),
('c', [300, 3]),
('a1', [101, 1]),
('a2', [102,