1

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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 5
    `dict_example[retrieve_random]`? – rassar Sep 12 '22 at 23:46
  • Does this answer your question? [How to get a random value from dictionary?](https://stackoverflow.com/questions/4859292/how-to-get-a-random-value-from-dictionary) – sj95126 Sep 12 '22 at 23:48
  • @sj95126 Unfortunately not. Thanks anyway, you are very kind –  Sep 12 '22 at 23:50
  • @rassar If I write retrieve = dict_example [retrieve_random], there is an error, because I created retrieve_random AFTER. Can you explain yourself better? Thank you –  Sep 12 '22 at 23:52

2 Answers2

1

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())
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • Thank you. Unfortunately your answer didn't help me, but I appreciate your help. Could you help me with this question about dictionaries? If you need help, I will be happy to accept your answer as a solution. Thank you https://stackoverflow.com/questions/73696767/problem-using-random-with-the-keys-of-two-dictionaries –  Sep 13 '22 at 01:47
1

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.

Shalofty
  • 29
  • 4