Dictionary So in python I have a dictionary that is composed of a name for the key and a class object associated with the name as the object. I need to append these objects to a list one by one from the dictionary, however when I attempt to use a for loop to do so it only appends the same object over and over agian. When I try to append dict.values(), I have them appended to a list but with the word dict_values being the first thing in the list when it should only be the values themselves. Does anybody have any ideas as to how to properly append these values? I have been using
for value in playerDict.values():
basicList.append(playerDict.values)
print(basicList)
to try and append the values to the list called basicList, however every time it appends the same object and after a few iterations the list simply looks like
[<built-in method values of dict object at 0x103a03d48>, <built-in method values of dict object at 0x103a03d48>, <built-in method values of dict object at 0x103a03d48>, <built-in method values of dict object at 0x103a03d48>]
Where am I going wrong in appending the object values. Sorry for the basic question I am pretty new to python.
Try doing this:
for value in playerDict.values():
basicList.append(value)
print(basicList)
Rather than:
for value in playerDict.values():
basicList.append(playerDict.values)
print(basicList)
When you try to append playerDict.values into the list, you were effectively trying to append value of the playerDict object in the object form and not what you stored in the key of the dictionary (and hence you were getting [<built-in method values of dict object at 0x103a03d48>, <built-in method values of dict object at 0x103a03d48> ). Whereas, when you append value, it appends the actual values of the keys stored in your dictionary.
Hope, it helps.
Related
i want to parse dictionary dynamically
here i'm trying to get the name of key dynamically and storing that key in variable and call dictionary using those variable
so far i tried this
var=users.to_dict()
z=var.keys()
y=z[0]
x=var[str(y)]['Scheme name']
when i pass str(y) it giving error
y=x[0]
TypeError: 'dict_keys' object does not support indexing
is there any better ways to this. please let me know
you cannot index 'dict_keys' but if you parse them to 'list'
var=users.to_dict()
z=list(var.keys())
y=z[0]
x=var[str(y)]['Scheme name']
it should work
y=z[0]
is where your error is.
var.keys() doesn't return a list, it returns a dict_keys, which doesn't support indexing. You can convert it to an list and then index:
y = list(x)[0]
However, z = list(var) will have the same effect (list of keys) and is a little more direct.
A dict_keys behaves this way because it's a view object. It's an iterable whose contents change if the underlying dictionary changes. The result of casting it as a list (or casting the dictionary as a list) is that it's no longer connected to dictionary.
the python program that i am writing calls to an api that returns this json:
Code Output
How do i access the subdetails? When i run the .keys() it only lists those three top levels. I want to be able to get specific items, e.g. "Utility"
I've tried several solutions but none parse correctly. I have tried calling the list inside the dictionary, to no avail. Originally i thought it was a dictionary inside of a dictionary, but Python thinks its a list nested into a dictionary.
Any help would be appreciated!
keys() function only returns the keys of dictionary, so it you call keys(), it will only return the three result. The "subdetails" you are referring to are the values of those keys. For key "SUMMARY" as an example, its value is a list instead of dict (note the "[" after the key). However, the list only has a single element. This is quite common in json. To retrive "Utility", all you need to do is data['SUMMARY'][0]['Utility']
Maybe to help you understand the data structure better, call the "values()" and "items()" function to see what it returns.
Since it's a dict of lists of dicts, simply use an index of 0 to access the first item of the list if there is always only one item in each list. For example, if your JSON object is stored as variable data, then the value of Utility can be accessed with data['SUMMARY'][0]['Utility'].
When I do dict.keys() in python3, I do not get a list, but an object of class dict_keys. Why is this and what can I do with this object? How to get the list?
Example code: type(dict(sape=4139, guido=4127, jack=4098).keys())
I wonder if this result is intentional and why?
I called matplotlib.plot(d.keys(),....) and got an error.
dict.keys returns a dict_keys object, which is an iterable object.
So, you can either convert it to a list using:
keys = list(dict.keys())
Or, you can simply iterate over the dict_keys object, like it was intended:
for key in dict.keys():
print(key)
In your example, it will print out:
sape
guido
jack
I need to create a dictionary from list of tuples. SQL query returns something like this
[['a',5], ['b',8], ['c',10]]
Is it possible to use python the dict() constructor ?
Go ahead and try:
dict([['a',5] , ['b',8] , ['c',10]])
on your favourite python console.
You'll notice: it works.
The docs tell you why:
dict(iterable, **kwarg)
Each item in the iterable must itself be an iterable with exactly two
objects. The first object of each item becomes a key in the new
dictionary, and the second object the corresponding value.
This obviously is under the assumption that a, b and c are what you actually wanted as keys in the dictionary.
I'm simply trying to access the output from a variable named parser which is outputting a dictionary of information in it like:
{u'param': [u'6'], u'booID': [u'911'], u'animal': [u'cat']}
How can I access each parameter, and loop through each outputting the key value?
I tried, a number of different things including:
for parsed_val in parser:
print parsed_val + parsed_val.keys()
but this throws a AttributeError of AttributeError: 'unicode' object has no attribute 'keys'.
Thank you!
for key, value in parser.iteritems():
print key, value
Iterating through a dictionary iterates through its keys.
You'll want to iterate over dict.items(), which iterates over a list of (key,pair) tuples.
You can also use dict.iteritems (on python 2.x), which avoids creating a list and iterates directly over the dictionary
Alternative method:
for key in parser.keys():
print(str(key) + ': '+str(parser[key]))
dict.keys() is a list of all keys.
dict.items() is a list of of key/value pairs.
dict.iteritems is an iterable object of dict.items().