How do I tell if the value of a particular key in my dictionary is a dictionary so that I can solve programming questions based on this recursively?How do you sort a dictionary by value?How do I sort a list of dictionaries by a value of the dictionary?Getting key with maximum value in dictionary?How to return multiple values from a function?How do I sort a dictionary by value?How to keep keys/values in same order as declared?How can I get dictionary key as variable directly in Python (not by searching from value)?Get key by value in dictionaryHow can I sort a dictionary by key?How to remove a key from a Python dictionary?
How would Japanese people react to someone refusing to say “itadakimasu” for religious reasons?
How do I run a script as sudo at boot time on Ubuntu 18.04 Server?
Background for black and white chart
When is the phrase "j'ai bon" used?
Can a non-invertible function be inverted by returning a set of all possible solutions?
Right indicator flash-frequency has increased and rear-right bulb is out
How can a flywheel makes engine runs smoothly?
Lead the way to this Literary Knight to its final “DESTINATION”
How to make all magic-casting innate, but still rare?
How can I maintain game balance while allowing my player to craft genuinely useful items?
Have Steve Rogers (Captain America) and a young Erik Lehnsherr (Magneto) interacted during WWII?
Leveraging cash for buying car
Catching a robber on one line
Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?
How to write a nice frame challenge?
How to search for Android apps without ads?
Using roof rails to set up hammock
Why can't I craft scaffolding in Minecraft 1.14?
Basic power tool set for Home repair and simple projects
Why can't we feel the Earth's revolution?
Explicit direct #include vs. Non-contractual transitive #include
Cut power on a remote Raspberry Pi 3 via another raspi
How did space travel spread through the galaxy?
Is the infant mortality rate among African-American babies in Youngstown, Ohio greater than that of babies in Iran?
How do I tell if the value of a particular key in my dictionary is a dictionary so that I can solve programming questions based on this recursively?
How do you sort a dictionary by value?How do I sort a list of dictionaries by a value of the dictionary?Getting key with maximum value in dictionary?How to return multiple values from a function?How do I sort a dictionary by value?How to keep keys/values in same order as declared?How can I get dictionary key as variable directly in Python (not by searching from value)?Get key by value in dictionaryHow can I sort a dictionary by key?How to remove a key from a Python dictionary?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
So I want to make sure that a particular value of key is a primitive data type i.e not a dictionary. If it is a dictionary, I want to recursively check till I reach the end. ( base case-: no dictionary in the values) Let's say I have this code
dict =
"Roll No" : "1",
"Car" :
"Ferrari" : "12",
"Pontiac" : "15"`
,
"Budget" : "3"
How do I get "Car" as the key which has the datatype of the values to be a dictionary?
And how do I access the sub-dictionary ?
"Car":
"Ferrari":"12",
"Pontiac":"15"
,
python dictionary object
add a comment |
So I want to make sure that a particular value of key is a primitive data type i.e not a dictionary. If it is a dictionary, I want to recursively check till I reach the end. ( base case-: no dictionary in the values) Let's say I have this code
dict =
"Roll No" : "1",
"Car" :
"Ferrari" : "12",
"Pontiac" : "15"`
,
"Budget" : "3"
How do I get "Car" as the key which has the datatype of the values to be a dictionary?
And how do I access the sub-dictionary ?
"Car":
"Ferrari":"12",
"Pontiac":"15"
,
python dictionary object
What's wrong withdict['Car']
?
– AkshayNevrekar
Mar 25 at 4:16
I dont think it would work. I mean I want to do it recursively so if there's subdictionaries possible within Car then I'd be stuck? if Car can be it's own dictionary which I can access then it'd work!
– Yash Shah
Mar 25 at 4:18
@AkshayNevrekar Actually dict['Car'] will be useful to get the values and check if they are in fact a dictionary and not a string. Thanks!
– Yash Shah
Mar 25 at 7:54
add a comment |
So I want to make sure that a particular value of key is a primitive data type i.e not a dictionary. If it is a dictionary, I want to recursively check till I reach the end. ( base case-: no dictionary in the values) Let's say I have this code
dict =
"Roll No" : "1",
"Car" :
"Ferrari" : "12",
"Pontiac" : "15"`
,
"Budget" : "3"
How do I get "Car" as the key which has the datatype of the values to be a dictionary?
And how do I access the sub-dictionary ?
"Car":
"Ferrari":"12",
"Pontiac":"15"
,
python dictionary object
So I want to make sure that a particular value of key is a primitive data type i.e not a dictionary. If it is a dictionary, I want to recursively check till I reach the end. ( base case-: no dictionary in the values) Let's say I have this code
dict =
"Roll No" : "1",
"Car" :
"Ferrari" : "12",
"Pontiac" : "15"`
,
"Budget" : "3"
How do I get "Car" as the key which has the datatype of the values to be a dictionary?
And how do I access the sub-dictionary ?
"Car":
"Ferrari":"12",
"Pontiac":"15"
,
python dictionary object
python dictionary object
edited Mar 25 at 4:17
Kingsley
4,03641431
4,03641431
asked Mar 25 at 4:11
Yash ShahYash Shah
123
123
What's wrong withdict['Car']
?
– AkshayNevrekar
Mar 25 at 4:16
I dont think it would work. I mean I want to do it recursively so if there's subdictionaries possible within Car then I'd be stuck? if Car can be it's own dictionary which I can access then it'd work!
– Yash Shah
Mar 25 at 4:18
@AkshayNevrekar Actually dict['Car'] will be useful to get the values and check if they are in fact a dictionary and not a string. Thanks!
– Yash Shah
Mar 25 at 7:54
add a comment |
What's wrong withdict['Car']
?
– AkshayNevrekar
Mar 25 at 4:16
I dont think it would work. I mean I want to do it recursively so if there's subdictionaries possible within Car then I'd be stuck? if Car can be it's own dictionary which I can access then it'd work!
– Yash Shah
Mar 25 at 4:18
@AkshayNevrekar Actually dict['Car'] will be useful to get the values and check if they are in fact a dictionary and not a string. Thanks!
– Yash Shah
Mar 25 at 7:54
What's wrong with
dict['Car']
?– AkshayNevrekar
Mar 25 at 4:16
What's wrong with
dict['Car']
?– AkshayNevrekar
Mar 25 at 4:16
I dont think it would work. I mean I want to do it recursively so if there's subdictionaries possible within Car then I'd be stuck? if Car can be it's own dictionary which I can access then it'd work!
– Yash Shah
Mar 25 at 4:18
I dont think it would work. I mean I want to do it recursively so if there's subdictionaries possible within Car then I'd be stuck? if Car can be it's own dictionary which I can access then it'd work!
– Yash Shah
Mar 25 at 4:18
@AkshayNevrekar Actually dict['Car'] will be useful to get the values and check if they are in fact a dictionary and not a string. Thanks!
– Yash Shah
Mar 25 at 7:54
@AkshayNevrekar Actually dict['Car'] will be useful to get the values and check if they are in fact a dictionary and not a string. Thanks!
– Yash Shah
Mar 25 at 7:54
add a comment |
1 Answer
1
active
oldest
votes
You can use python's type()
function to decide if the element is a dictionary, and thus when to recurse:
searchDictionary( haystack, needle ):
for key in haystack.keys():
if type( haystack[key] ) is dict:
searchDictionary( haystack[key], needle )
elif type( haystack[key] ) is str:
# TODO - match string
if ( haystack[key] == needle ):
print( "Found at "+str(key) )
elif type( haystack[key] ) is int:
# TODO - match integer
pass
There's a bunch of other ways to iterate over the dictionary, but I chose this method to illustrate the answer in a readable manner.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331167%2fhow-do-i-tell-if-the-value-of-a-particular-key-in-my-dictionary-is-a-dictionary%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use python's type()
function to decide if the element is a dictionary, and thus when to recurse:
searchDictionary( haystack, needle ):
for key in haystack.keys():
if type( haystack[key] ) is dict:
searchDictionary( haystack[key], needle )
elif type( haystack[key] ) is str:
# TODO - match string
if ( haystack[key] == needle ):
print( "Found at "+str(key) )
elif type( haystack[key] ) is int:
# TODO - match integer
pass
There's a bunch of other ways to iterate over the dictionary, but I chose this method to illustrate the answer in a readable manner.
add a comment |
You can use python's type()
function to decide if the element is a dictionary, and thus when to recurse:
searchDictionary( haystack, needle ):
for key in haystack.keys():
if type( haystack[key] ) is dict:
searchDictionary( haystack[key], needle )
elif type( haystack[key] ) is str:
# TODO - match string
if ( haystack[key] == needle ):
print( "Found at "+str(key) )
elif type( haystack[key] ) is int:
# TODO - match integer
pass
There's a bunch of other ways to iterate over the dictionary, but I chose this method to illustrate the answer in a readable manner.
add a comment |
You can use python's type()
function to decide if the element is a dictionary, and thus when to recurse:
searchDictionary( haystack, needle ):
for key in haystack.keys():
if type( haystack[key] ) is dict:
searchDictionary( haystack[key], needle )
elif type( haystack[key] ) is str:
# TODO - match string
if ( haystack[key] == needle ):
print( "Found at "+str(key) )
elif type( haystack[key] ) is int:
# TODO - match integer
pass
There's a bunch of other ways to iterate over the dictionary, but I chose this method to illustrate the answer in a readable manner.
You can use python's type()
function to decide if the element is a dictionary, and thus when to recurse:
searchDictionary( haystack, needle ):
for key in haystack.keys():
if type( haystack[key] ) is dict:
searchDictionary( haystack[key], needle )
elif type( haystack[key] ) is str:
# TODO - match string
if ( haystack[key] == needle ):
print( "Found at "+str(key) )
elif type( haystack[key] ) is int:
# TODO - match integer
pass
There's a bunch of other ways to iterate over the dictionary, but I chose this method to illustrate the answer in a readable manner.
answered Mar 25 at 4:25
KingsleyKingsley
4,03641431
4,03641431
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331167%2fhow-do-i-tell-if-the-value-of-a-particular-key-in-my-dictionary-is-a-dictionary%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's wrong with
dict['Car']
?– AkshayNevrekar
Mar 25 at 4:16
I dont think it would work. I mean I want to do it recursively so if there's subdictionaries possible within Car then I'd be stuck? if Car can be it's own dictionary which I can access then it'd work!
– Yash Shah
Mar 25 at 4:18
@AkshayNevrekar Actually dict['Car'] will be useful to get the values and check if they are in fact a dictionary and not a string. Thanks!
– Yash Shah
Mar 25 at 7:54