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

            How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

            Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?