Dictionary changing key value Python [duplicate] - python

This question already has answers here:
Change the name of a key in dictionary
(23 answers)
Closed 6 years ago.
I have got a dictionary in the form of:
{0.1: (0.7298579,0.7987254)}
which corresponds to: {test_size: (train_error, test_error)}.
I would like to change the key value test_size into 1 - test_size. So that we obtain:
{0.9: (0.7298579, 0.7987254)}
How can I do this?

You can do this :
>>> d = {0.1:(0.7298579,0.7987254)}
>>> new_d = {1-k: v for k, v in d.items()}
>>> new_d
{0.9: (0.7298579, 0.7987254)}

If the dict is not horribly huge, you can just create a new dict and copy the old one with new keys.

Related

How can I create a dictionary from 2 lists, one representing keys and the other values? [duplicate]

This question already has answers here:
How can I make a dictionary (dict) from separate lists of keys and values?
(21 answers)
Closed 2 years ago.
As the question says, I have two lists that look like this:
list1 = ["key1", "key2"]
list2 = ["value1", "value2"]
I would like to create a dictionary like this:
dict1 = {"key1": "value1" , "key2" : "value2"}
Is there are an easy way to do so?
my_dict = dict(zip(list1, list2))
A dictionary comprehension is perfect for this:
dict1 = {k: v for k, v in zip(list1, list2)}
Edit: it seems there is an even shorter solution, as per Sazzy's answer :)

How flatten semi nested python dictionary [duplicate]

This question already has answers here:
Flatten nested dictionaries, compressing keys
(32 answers)
Closed 3 years ago.
I have a python dictionary like following:
score_dictionary={'Agriculture':89,'Health':{'Public':90,'Private':78},'Mines':70,'Commerce':67}
Using this dictionary, I want to convert to the following:
score_dictionary={'Agriculture':89,'Health_public':90,'Health_Private':78,'Mines':70,'Commerce':67}
Now I'm stuck at how to convert it?
You could use isinstance to check whether or not the value in a given key/value pair consist in another dictionary, format the key name with string formatting and update accordingly:
d = {}
for k,v in score_dictionary.items():
if not isinstance(v, dict):
d[k] = v
else:
for k_, v_ in v.items():
d[f'{k}_{k_}'] = v_
print(d)
{'Agriculture': 89,
'Health_Public': 90,
'Health_Private': 78,
'Mines': 70,
'Commerce': 67}

How to get the key from value in a dictionary in Python? [duplicate]

This question already has answers here:
Get key by value in dictionary
(43 answers)
Closed 5 years ago.
d[key] = value
but how to get the keys from value?
For example:
a = {"horse": 4, "hot": 10, "hangover": 1, "hugs": 10}
b = 10
print(do_something with 10 to get ["hot", "hugs"])
You can write a list comprehension to pull out the matching keys.
print([k for k,v in a.items() if v == b])
Something like this can do it:
for key, value in a.iteritems():
if value == 10:
print key
If you want to save the associated keys to a value in a list, you edit the above example as follows:
keys = []
for key, value in a.iteritems():
if value == 10:
print key
keys.append(key)
You can also do that in a list comprehension as pointed out in an other answer.
b = 10
keys = [key for key, value in a.iteritems() if value == b]
Note that in python 3, dict.items is equivalent to dict.iteritems in python 2, check this for more details: What is the difference between dict.items() and dict.iteritems()?

Python: How to get the updated dictionary in 1 line? [duplicate]

This question already has answers here:
How do I merge two dictionaries in a single expression in Python?
(43 answers)
Closed 6 years ago.
In python, I have a dictionary named dict_a :
dict_a = {'a':1}
and I want to get a new dictionary dict_b as the update of dict_a,at the same time I don't want to change the dict_a, so I used this:
dict_b = copy.deepcopy(dict_a).update({'b':2})
However, the return value of dict.update is None, so the above code doesn't work. And I have to use this:
temp = copy.deepcopy(dict_a)
temp.update({'b':2})
dict_b = temp
The question is, how to get my goal in one line? Or some
What about:
>>> a = {'a':1}
>>> update = {'b': 1}
>>> b = dict(a.items() + update.items())
>>> b
{'a': 1, 'b': 1}
update - is your value that need to update(extend) dict a
b - is a new resulting dict
Anfd in this case a stay unchanged

How do you return a new dictionary that only contains a certain type? [duplicate]

This question already has answers here:
How do I filter out non-string keys in a dictionary in Python?
(5 answers)
Closed 7 years ago.
How can I specify a new dictionary to only append certain types. For example if my old list was {'hello': 1, 2:3, 3.0:'hi'}, how could I make my new list check for the type and then only append that type in it. So new_dict[int] = {2:3} or new_dict[str] = {'hello': 1}?
How about this
d = {'hello': 1, 2:3, 3.0:'hi'}
final_d = {k: v for k, v in d.items() if isinstance(v, int) and isinstance(k,int)}

Categories

Resources