Skip to content

Tag: dictionary

indexing nested dictionary elements

I am trying to access the dictionary elements in python, which looks like this: I want to access the value of key ‘xyz’.Is there any direct method to access it in python. Any help is greatly appreciated. Answer In steps, mydict[‘result’] returns [{‘abc’:1,’xyz’:…

Way to call method depending on variable?

I already have a working, but in my oppinion not beautiful solution for a part of a long script. My script uses several similar methods, that differ too much to combine. However I came to a point where I want to call one of those methods depending on a given variable. The names of the methods are build up lik…

String by value; Dictionary by reference?

I’m coming from C, learning Python. In Python (3.5.2), it seems that assignment of one data type to another of the same type is sometimes done by value and sometimes by reference. For example, strings are assigned by value: This is the behavior I expect. However, it works differently with dictionaries: …

Iterate through a list inside a dictionary in python

I’m very new to Python and I need to create a function that divides by 2 the values in the lists inside the dictionary: desired output: I found this post python: iterating through a dictionary with list values which helped a little, but unfortunately is the “whatever” part of the example cod…

Convert dictionary into list with length based on values

I have a dictionary I want to convert it to a list that holds the keys of the dictionary. Each key should be repeated as many times as its associated value. I’ve written this code that does the job: Output: But I would like it to be a list comprehension. How can this be done? Answer You can do it