How to iterate thorugh JSON/dict in python3? [duplicate] - python

This question already has answers here:
Iterating over dictionaries using 'for' loops
(15 answers)
Closed 3 years ago.
I'm sending request to an API using python's requests module and the response is in JSON (can be called dictionary right?)
If I do
for i in response:
print(i)
It would only print the key (the parameter) and not the value, how to get both as output. Thanks.

You can call items to loop over the key/value pairs at the same time.
for key,value in response.items():
print(key, value)
The items method returns an iterator over the key/value pairs of a dictionary.

Related

Dictionary value is different in input and different in output [duplicate]

This question already has answers here:
How to keep keys/values in same order as declared?
(13 answers)
Closed 4 years ago.
I have some error in Python 3 while using dictionaries. The input and output does not match
What you are getting is not an error. Read about dictionaries first: https://www.w3schools.com/python/python_dictionaries.asp
Dictionaries don't work as list. They do not have order. They are hashed data structure that strongly binds keys with value. 5 will always be bound with "five" and 4 will always be bound with "four". If you type dict1[5], you will always get 'five'. In dictionaries, order of arrangement is not important, because python uses complex algorithms to keep key - value bound by hashing, and these algorithms may alter the order of arrangement, but order of arrangement is anyways not important for us in dictionaries.
Never use dictionaries as lists. Dictionaries are collection of key value pairs and you access a value by a key. Lists are like arrays, you access a value by index.

Python, access data Dictionary in Dictionary [duplicate]

This question already has answers here:
Accessing dictionary of dictionary in python
(7 answers)
Closed 6 years ago.
I have data like this :
As you can see : dictionary:list[{dictionary1},{dictionary2} ,... ]
How i can create loop to access all 'type' inside of every small dictionary ?
Thanks very much !
I had an answer after read instructions,
i can access data with code like this :
for item in result_records:
getValues = result_records.get(item) # Return list of dictionary
for subDict in getValues: # With every dictionary
if(subDict['type'] == 'VIEW_ITEM'):
Thanks all very much for helping me !

Delete Item from specific location in list in Python [duplicate]

This question already has answers here:
How to remove an element from a list by index
(18 answers)
Closed 8 years ago.
I'm writing a statistics program which will maintain a list of float values. The program will implement the following menu:
Add value to list
Delete value from list (by value)
Delete value from list (by location in list)
Display list
Exit
I've written everything except the third option. I can't figure out how to get it done. How would one do that?
In Python you can delete an item from a list by referencing it. Like this:
del list[location]

python default dictionary calling key values [duplicate]

This question already has answers here:
Python Dictionary DataStructure which method d[] or d.get()?
(5 answers)
Closed 8 years ago.
I have written a bit of code using
setting value
dic["key"] = "someval"
and fetching it the same way
print dic["key"]
then I discovered that an alternative way to fetch a dictionary value is to use
print dic.get("key")
I want all my code to use the same method, so should I rewrite all using dic.get("key") ?
If you have a flat dictionary and you want to add or modify a key/value pair, than the best way is to use a simple assignment:
h[key] = value
The get method is useful when you want to get a value from a dictionary for existing keys or use a default value for non-existing keys:
print h.get(key, "Does not exist!")

adding new pair in dictionary why the last added 'mynewkey': 'mynewvalue' came first in dictionary [duplicate]

This question already has answers here:
Why is python ordering my dictionary like so? [duplicate]
(3 answers)
How to keep keys/values in same order as declared?
(13 answers)
Closed 9 years ago.
>>> d = {'key':'value'}
>>> print d
{'key': 'value'}
>>> d['mynewkey'] = 'mynewvalue'
>>> print d
{'mynewkey': 'mynewvalue', 'key': 'value'}
why the last added 'mynewkey': 'mynewvalue' came first in dictionary
Python dictionaries are not ordered. If you iterate over the items of a dict object you will not get them in the order you inserted them. The reason is the internal data structure behind it.
If you use Python 2.7 or Python 3.3 you can resort to http://docs.python.org/dev/library/collections.html#collections.OrderedDict
Dictionaries and sets are unordered in Python. The items end up in an order that varies from Python version to Python version, implementation to implementation, and shouldn't be relied upon.
If you need to keep the items in order, you can use a collections.OrderedDict. For versions of Python older than 2.7, you can download it from PyPI.

Categories