Python Forum
How to get python to select a dictionary.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get python to select a dictionary.
#1
I'm not even sure how to word my question. I know exactly what I want to do but I don't know how to ask it so I will try an example.

Suppose I have multiple dictionaries with the same keys but different values. I want to be able to programmatic change the name of the dictionary that is being searched. What I DON'T want to have to do is type something like look in this one, then this one, then this one, then this one and have to type the name into the code every time. What I want to do is something more like name each dictionary with a similar name but perhaps a single letter different. Such as this:
dict01 and dict02 are seperate dictionary files. I want to look for the key called 'myKey'.

So I have a list called dictList.
['dict01', 'dict02']

maybe I can do something like:
for x in dictList:
print (x['myKey'])
Can I even do something like this?
Reply
#2
You can. Have you tried it?
>>> d_a = {'myKey': 'a'}
>>> d_b = {'myKey': 'b'}
>>> for d in [d_a, d_b]:
...     print(d['myKey'])
... 
a
b
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error using mariadb select query with form in python? shams 2 2,000 Jul-29-2021, 12:30 PM
Last Post: shams
  Python and Postgresql syntax select statement Nesreenmhd 1 4,983 Sep-07-2019, 06:08 PM
Last Post: ndc85430
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 50,149 Apr-27-2019, 03:13 AM
Last Post: perfringo
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 2,233 Feb-14-2019, 08:34 PM
Last Post: woooee
  user input to select and print data from another class python TyTheChosenOne 6 4,122 Aug-30-2018, 05:53 PM
Last Post: TyTheChosenOne

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020