Can I randomly retrieve the key of a dictionary including its values? Retrieve only the key, with no values

Question:

I would like to get a random key of a dictionary, but it also includes its corresponding value.

I am using this, however I extract only the keyword and not its value.

import random

dict_example = {
    
            "name 1" : {
            "born" : "London",
            "residence" : "Brighton",
            },
            
            "name 2" : {
            "born" : "Paris",
            "residence" : "Nice",
            },
    
        }

retrieve = list(dict_example.keys()) 
retrieve_random = random.choice(retrieve)
print(retrieve_random)

The random output happens correctly, but I have to "compare" the value of the key with the value of another key, so using this way I can’t, because it’s as if I recovered only the word name 1 or name 2 without its corresponding value.

Probably the problem is recovers = list (dict_example.keys ()).

How can I also retrieve the value of each key?

Asked By: Max_98

||

Answers:

use the items() of the dictionary to form a list, as shown below.

change this line retrieve = list(dict_example.keys())

to

retrieve = list(dict_example.items())
Answered By: Pavan Chandaka

I’m not entirely sure of the scope of your project. In the case that the example you included is simple in comparison to your project, what I would suggest is looking into JSON.

Selecting random values from a json file

Retrieving key, value of json data python

JSON is great for stuff like this. You can build a db using JSON and import it into your Py file easily. I would read into the structure of JSON. Once you get a decent understand of building a correct, complex JSON file, you’ll be able to use Py logic to parse through it.


    {
      "listStates": [
        {
          "stateName": "ALABAMA",
          "filingStatus": [
            {
              "status": "SINGLE",
              "incomeBrackets": [
                {
                  "incomeMin": 0,
                  "incomeMax": 499,
                  "taxRate": 0.02
                },
                {
                  "incomeMin": 500,
                  "incomeMax": 2999,
                  "taxRate": 0.04
                },
                {
                  "incomeMin": 3000,
                  "incomeMax": 10000000000,
                  "taxRate": 0.05
                }
              ]
            },
            {
              "status": "MARRIED FILING JOINTLY",
              "incomeBrackets": [
                {
                  "incomeMin": 0,
                  "incomeMax": 999,
                  "taxRate": 0.02
                },
                {
                  "incomeMin": 1000,
                  "incomeMax" : 5999,
                  "taxRate": 0.04
                },
                {
                  "incomeMin": 6000,
                  "incomeMax": 10000000000,
                  "taxRate": 0.05
                }
              ]
            }
          ]
        },
        {
          "stateName": "ALASKA",
          "filingStatus": [
            {
              "status": "SINGLE",
              "incomeBrackets": [
                {
                  "income": 0,
                  "taxRate": 0.0
                }
              ]
            },
            {
              "status": "MARRIED FILING JOINTLY",
              "incomeBrackets": [
                {
                  "income": 0,
                  "taxRate": 0.0
                },
                {
                  "income": 0,
                  "taxRate": 0.0
                }
              ]
            }
          ]
        },
        {
          "stateName": "ARIZONA",
          "filingStatus": [
            {
              "status": "SINGLE",
              "incomeBrackets": [
                {
                  "income": 0,
                  "taxRate": 0.0259
                },
                {
                  "income": 27808,
                  "taxRate": 0.0334
                },
                {
                  "income": 55615,
                  "taxRate": 0.0417
                },
                {
                  "income": 166843,
                  "taxRate": 0.0450
                }
              ]
            },
    }

Here is an example of iterating through the code above

i = 0
for states in data['listStates'][i]['stateName']:
    if residence == data['listStates'][i]['stateName']:
        print("1st for loop works")
        i = i
        print(data['listStates'][i]['stateName'])
    else:
        i += 1

As I said, this all depends on the scope of your project. But from what I understand is that Python reads the JSON data as if it were a dictionary. This is case specific code that I used here just so you’d get the idea.

Answered By: Shalofty
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.