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;








-1















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"
,










share|improve this question
























  • 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

















-1















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"
,










share|improve this question
























  • 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













-1












-1








-1


0






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"
,










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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.






    share|improve this answer



























      0














      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.






      share|improve this answer

























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 4:25









        KingsleyKingsley

        4,03641431




        4,03641431





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript