OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to random choose an item of a list, that is a value of a key, within a dictionary, within a dictionary, within a dictionary, using Python [closed]

  • Thread starter Thread starter Victor
  • Start date Start date
V

Victor

Guest
I'm using Python for a project. I do have a lists of items, that I want to be able to randomly choose. This lists are nested within 5 levels of dictionaries.

Code:
# Example dictionary
nested_dict = {
    'level1': {
        'level2': {
            'level3': {
                'level4': {
                     'level5': {
                           'key': ['item1', 'item2', 'item3', 'item4']
    'level1.1': {
                  }
    'level1.2': {
                  }

               }
            }
        }
    }
}
  1. My question is how do I reach these lists, to randomly choose an individual item from any of these list?
  2. At every level, on vertical we could have "N" dictionaries (like: "level1.1", "level1.2", ... "level1.N"), and on an horizontal we can have "M" levels (like: "level1", "level2", ... "levelM"). Knowing that based on user input, the user can choose to what dictionary from this nested dictionary structure to go, how it will look the code using IF/ELSE statements, at the end of which there will used the random function, to get the element from the list?
  3. Will alternatively be a better way to structure this data, in order to easier and more efficient reach any random value of these nested lists, instead of a such a complex dictionary structure?

I've tried to use a classes, or objects, but in the end I decided to use dictionaries for this small project. Also I'm considering to store all the data in an SQL, but at moment I need to work with a dictionary and locally. I'm open to suggestions how to better approach this. Thank you!
<p>I'm using Python for a project.
I do have a lists of items, that I want to be able to randomly choose.
This lists are nested within 5 levels of dictionaries.</p>
<pre><code># Example dictionary
nested_dict = {
'level1': {
'level2': {
'level3': {
'level4': {
'level5': {
'key': ['item1', 'item2', 'item3', 'item4']
'level1.1': {
}
'level1.2': {
}

}
}
}
}
}
</code></pre>
<ol>
<li>My question is how do I reach these lists, to randomly choose an individual item from any of these list?</li>
<li>At every level, on vertical we could have "N" dictionaries (like: "level1.1", "level1.2", ... "level1.N"), and on an horizontal we can have "M" levels (like: "level1", "level2", ... "levelM").
Knowing that based on user input, the user can choose to what dictionary from this nested dictionary structure to go, how it will look the code using IF/ELSE statements, at the end of which there will used the random function, to get the element from the list?</li>
<li>Will alternatively be a better way to structure this data, in order to easier and more efficient reach any random value of these nested lists, instead of a such a complex dictionary structure?</li>
</ol>
<p>I've tried to use a classes, or objects, but in the end I decided to use dictionaries for this small project. Also I'm considering to store all the data in an SQL, but at moment I need to work with a dictionary and locally.
I'm open to suggestions how to better approach this. Thank you!</p>
 

Latest posts

Top