look for json key to print it's content without brackets in python












2















i'm relatively new to python/programming and even more of a noob in json.



I am making a dictionary app for a side projects that I have, it works fine, I can search for words and get their definition, but I want to make it perfect and I want the results to be readable for the final user (I know about indenting but I don't want the brackets and all the json formatting to appear in the results)



So this is the json i'm pulling the data from:



{
"Bonjour": {
"English Word": "Hello",
"Type of word": "whatever",
"Defintion": "Means good day",
"Use case example": "Bonjour igo",
"Additional information": "BRO"
}
}


and this is the code i'm using to get the values (it doesn't work), the "search" variable is = to "Bonjour" in this case (it's a user input)



currentword = json.load(data) #part of the "with open..."


for definition in currentword[search]['English Word', 'Definition', 'Use case example']:
print(definition)


the error I get is the following:



KeyError: ('English Word', 'Definition', 'Use case example')


Now i'm unsure if "Bonjour" is the key or "English Word", etc... are the keys, if not, what is "Bonjour"



Anyways, I want it to print the values of "English Word" and preferably as "English Word - VALUE/DEFINITION"



Thanks for any help










share|improve this question





























    2















    i'm relatively new to python/programming and even more of a noob in json.



    I am making a dictionary app for a side projects that I have, it works fine, I can search for words and get their definition, but I want to make it perfect and I want the results to be readable for the final user (I know about indenting but I don't want the brackets and all the json formatting to appear in the results)



    So this is the json i'm pulling the data from:



    {
    "Bonjour": {
    "English Word": "Hello",
    "Type of word": "whatever",
    "Defintion": "Means good day",
    "Use case example": "Bonjour igo",
    "Additional information": "BRO"
    }
    }


    and this is the code i'm using to get the values (it doesn't work), the "search" variable is = to "Bonjour" in this case (it's a user input)



    currentword = json.load(data) #part of the "with open..."


    for definition in currentword[search]['English Word', 'Definition', 'Use case example']:
    print(definition)


    the error I get is the following:



    KeyError: ('English Word', 'Definition', 'Use case example')


    Now i'm unsure if "Bonjour" is the key or "English Word", etc... are the keys, if not, what is "Bonjour"



    Anyways, I want it to print the values of "English Word" and preferably as "English Word - VALUE/DEFINITION"



    Thanks for any help










    share|improve this question



























      2












      2








      2








      i'm relatively new to python/programming and even more of a noob in json.



      I am making a dictionary app for a side projects that I have, it works fine, I can search for words and get their definition, but I want to make it perfect and I want the results to be readable for the final user (I know about indenting but I don't want the brackets and all the json formatting to appear in the results)



      So this is the json i'm pulling the data from:



      {
      "Bonjour": {
      "English Word": "Hello",
      "Type of word": "whatever",
      "Defintion": "Means good day",
      "Use case example": "Bonjour igo",
      "Additional information": "BRO"
      }
      }


      and this is the code i'm using to get the values (it doesn't work), the "search" variable is = to "Bonjour" in this case (it's a user input)



      currentword = json.load(data) #part of the "with open..."


      for definition in currentword[search]['English Word', 'Definition', 'Use case example']:
      print(definition)


      the error I get is the following:



      KeyError: ('English Word', 'Definition', 'Use case example')


      Now i'm unsure if "Bonjour" is the key or "English Word", etc... are the keys, if not, what is "Bonjour"



      Anyways, I want it to print the values of "English Word" and preferably as "English Word - VALUE/DEFINITION"



      Thanks for any help










      share|improve this question
















      i'm relatively new to python/programming and even more of a noob in json.



      I am making a dictionary app for a side projects that I have, it works fine, I can search for words and get their definition, but I want to make it perfect and I want the results to be readable for the final user (I know about indenting but I don't want the brackets and all the json formatting to appear in the results)



      So this is the json i'm pulling the data from:



      {
      "Bonjour": {
      "English Word": "Hello",
      "Type of word": "whatever",
      "Defintion": "Means good day",
      "Use case example": "Bonjour igo",
      "Additional information": "BRO"
      }
      }


      and this is the code i'm using to get the values (it doesn't work), the "search" variable is = to "Bonjour" in this case (it's a user input)



      currentword = json.load(data) #part of the "with open..."


      for definition in currentword[search]['English Word', 'Definition', 'Use case example']:
      print(definition)


      the error I get is the following:



      KeyError: ('English Word', 'Definition', 'Use case example')


      Now i'm unsure if "Bonjour" is the key or "English Word", etc... are the keys, if not, what is "Bonjour"



      Anyways, I want it to print the values of "English Word" and preferably as "English Word - VALUE/DEFINITION"



      Thanks for any help







      python json






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 19 at 9:18







      Flexy

















      asked Jan 19 at 9:11









      FlexyFlexy

      115




      115
























          3 Answers
          3






          active

          oldest

          votes


















          2














          From your problem, looks like you want to extract only some of the key-value pairs from your existing dictionary.



          Try like below:



          data = {
          "Bonjour": {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Definition": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }
          }

          currentword = data
          search = "Bonjour"

          result = dict((k, currentword[search][k]) for k in ['English Word', 'Definition', 'Use case example'])

          for k,v in result.items():
          print k + ":" + v


          Result:



          Definition:Means good day 
          English Word:Hello
          Use case example:Bonjour igo





          share|improve this answer


























          • thanks for showing me the way to do it, but could you explain why it is like this? for example why can't I use search directly instead of using "search_word" variable Thanks

            – Flexy
            Jan 19 at 9:25











          • You need to use search_word, it gives you the dictionary related to "Bonjour" alone. Current_word contains the full dictionary and I assume it will have many words like Bonjour.

            – Jay
            Jan 19 at 9:26











          • And also i'd like to not have the brackets and have each definition on a separate line

            – Flexy
            Jan 19 at 9:27











          • Check my edited post. I think this should do the trick.

            – Jay
            Jan 19 at 9:32






          • 1





            @Flexy, if the answer helped you, please consider upvoting too. Thanks!

            – Jay
            Jan 19 at 9:34



















          3














          JSON format is simply a nice way to pair keys and values.
          Keys are the names we give to Values, so it will be easy to access them.

          If we took your JSON, and split it by keys and values, this is what we would get:

          Keys: "Bonjour", "English Word", "Type of word", "Defintion", "Use case example", "Additional information".



          Showing all values is a little complex, so I'll explain:

          The value of "Bonjour" is this:



          {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Defintion": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }


          And all other value are described in the value of "Bonjour".

          The value of "English Word" is "Hello" and so on.



          When you write a line like so: currentword[search]['English Word', 'Definition', 'Use case example'], you are telling Python to look for a key named ('English Word', 'Definition', 'Use case example'), and obviously it does not exist.



          What you should do is as follows:



          for definition in currentword[search]:
          eng_word = definition['English Word']
          print('English Word - {}'.format(eng_word))


          please note that definition contain all other fields as well, so you can choose whichever one you like.






          share|improve this answer
























          • thanks you for helping, I wish I could give you upvote but i'm too new

            – Flexy
            Jan 19 at 9:42



















          1














          This line:



          currentword[search]['English Word', 'Definition', 'Use case example']


          Calls 'English Word', 'Definition', 'Use case example' as a tuple key from the inner dict, which doesn't exist in your dictionary, which is why a KeyError is raised.



          If you want just the english word, use this instead:



          currentword[search]["English Word"]


          Assuming search is "Bonjour".



          It also looks like you are also trying to filter out specific keys from the inner dict separately. If this is the case, you can do this:



          d = {
          "Bonjour": {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Defintion": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }
          }

          inner_dict = d['Bonjour']

          keys = ["English Word", "Use case example", "Defintion"]

          print({k: inner_dict[k] for k in keys})
          # {'English Word': 'Hello', 'Use case example': 'Bonjour igo', 'Defintion': 'Means good day'}





          share|improve this answer
























          • I have not verified it because the the code posted by another person worked, but still, thanks

            – Flexy
            Jan 19 at 9:50











          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%2f54265592%2flook-for-json-key-to-print-its-content-without-brackets-in-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          From your problem, looks like you want to extract only some of the key-value pairs from your existing dictionary.



          Try like below:



          data = {
          "Bonjour": {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Definition": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }
          }

          currentword = data
          search = "Bonjour"

          result = dict((k, currentword[search][k]) for k in ['English Word', 'Definition', 'Use case example'])

          for k,v in result.items():
          print k + ":" + v


          Result:



          Definition:Means good day 
          English Word:Hello
          Use case example:Bonjour igo





          share|improve this answer


























          • thanks for showing me the way to do it, but could you explain why it is like this? for example why can't I use search directly instead of using "search_word" variable Thanks

            – Flexy
            Jan 19 at 9:25











          • You need to use search_word, it gives you the dictionary related to "Bonjour" alone. Current_word contains the full dictionary and I assume it will have many words like Bonjour.

            – Jay
            Jan 19 at 9:26











          • And also i'd like to not have the brackets and have each definition on a separate line

            – Flexy
            Jan 19 at 9:27











          • Check my edited post. I think this should do the trick.

            – Jay
            Jan 19 at 9:32






          • 1





            @Flexy, if the answer helped you, please consider upvoting too. Thanks!

            – Jay
            Jan 19 at 9:34
















          2














          From your problem, looks like you want to extract only some of the key-value pairs from your existing dictionary.



          Try like below:



          data = {
          "Bonjour": {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Definition": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }
          }

          currentword = data
          search = "Bonjour"

          result = dict((k, currentword[search][k]) for k in ['English Word', 'Definition', 'Use case example'])

          for k,v in result.items():
          print k + ":" + v


          Result:



          Definition:Means good day 
          English Word:Hello
          Use case example:Bonjour igo





          share|improve this answer


























          • thanks for showing me the way to do it, but could you explain why it is like this? for example why can't I use search directly instead of using "search_word" variable Thanks

            – Flexy
            Jan 19 at 9:25











          • You need to use search_word, it gives you the dictionary related to "Bonjour" alone. Current_word contains the full dictionary and I assume it will have many words like Bonjour.

            – Jay
            Jan 19 at 9:26











          • And also i'd like to not have the brackets and have each definition on a separate line

            – Flexy
            Jan 19 at 9:27











          • Check my edited post. I think this should do the trick.

            – Jay
            Jan 19 at 9:32






          • 1





            @Flexy, if the answer helped you, please consider upvoting too. Thanks!

            – Jay
            Jan 19 at 9:34














          2












          2








          2







          From your problem, looks like you want to extract only some of the key-value pairs from your existing dictionary.



          Try like below:



          data = {
          "Bonjour": {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Definition": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }
          }

          currentword = data
          search = "Bonjour"

          result = dict((k, currentword[search][k]) for k in ['English Word', 'Definition', 'Use case example'])

          for k,v in result.items():
          print k + ":" + v


          Result:



          Definition:Means good day 
          English Word:Hello
          Use case example:Bonjour igo





          share|improve this answer















          From your problem, looks like you want to extract only some of the key-value pairs from your existing dictionary.



          Try like below:



          data = {
          "Bonjour": {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Definition": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }
          }

          currentword = data
          search = "Bonjour"

          result = dict((k, currentword[search][k]) for k in ['English Word', 'Definition', 'Use case example'])

          for k,v in result.items():
          print k + ":" + v


          Result:



          Definition:Means good day 
          English Word:Hello
          Use case example:Bonjour igo






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 19 at 9:28

























          answered Jan 19 at 9:20









          JayJay

          14.5k2163120




          14.5k2163120













          • thanks for showing me the way to do it, but could you explain why it is like this? for example why can't I use search directly instead of using "search_word" variable Thanks

            – Flexy
            Jan 19 at 9:25











          • You need to use search_word, it gives you the dictionary related to "Bonjour" alone. Current_word contains the full dictionary and I assume it will have many words like Bonjour.

            – Jay
            Jan 19 at 9:26











          • And also i'd like to not have the brackets and have each definition on a separate line

            – Flexy
            Jan 19 at 9:27











          • Check my edited post. I think this should do the trick.

            – Jay
            Jan 19 at 9:32






          • 1





            @Flexy, if the answer helped you, please consider upvoting too. Thanks!

            – Jay
            Jan 19 at 9:34



















          • thanks for showing me the way to do it, but could you explain why it is like this? for example why can't I use search directly instead of using "search_word" variable Thanks

            – Flexy
            Jan 19 at 9:25











          • You need to use search_word, it gives you the dictionary related to "Bonjour" alone. Current_word contains the full dictionary and I assume it will have many words like Bonjour.

            – Jay
            Jan 19 at 9:26











          • And also i'd like to not have the brackets and have each definition on a separate line

            – Flexy
            Jan 19 at 9:27











          • Check my edited post. I think this should do the trick.

            – Jay
            Jan 19 at 9:32






          • 1





            @Flexy, if the answer helped you, please consider upvoting too. Thanks!

            – Jay
            Jan 19 at 9:34

















          thanks for showing me the way to do it, but could you explain why it is like this? for example why can't I use search directly instead of using "search_word" variable Thanks

          – Flexy
          Jan 19 at 9:25





          thanks for showing me the way to do it, but could you explain why it is like this? for example why can't I use search directly instead of using "search_word" variable Thanks

          – Flexy
          Jan 19 at 9:25













          You need to use search_word, it gives you the dictionary related to "Bonjour" alone. Current_word contains the full dictionary and I assume it will have many words like Bonjour.

          – Jay
          Jan 19 at 9:26





          You need to use search_word, it gives you the dictionary related to "Bonjour" alone. Current_word contains the full dictionary and I assume it will have many words like Bonjour.

          – Jay
          Jan 19 at 9:26













          And also i'd like to not have the brackets and have each definition on a separate line

          – Flexy
          Jan 19 at 9:27





          And also i'd like to not have the brackets and have each definition on a separate line

          – Flexy
          Jan 19 at 9:27













          Check my edited post. I think this should do the trick.

          – Jay
          Jan 19 at 9:32





          Check my edited post. I think this should do the trick.

          – Jay
          Jan 19 at 9:32




          1




          1





          @Flexy, if the answer helped you, please consider upvoting too. Thanks!

          – Jay
          Jan 19 at 9:34





          @Flexy, if the answer helped you, please consider upvoting too. Thanks!

          – Jay
          Jan 19 at 9:34













          3














          JSON format is simply a nice way to pair keys and values.
          Keys are the names we give to Values, so it will be easy to access them.

          If we took your JSON, and split it by keys and values, this is what we would get:

          Keys: "Bonjour", "English Word", "Type of word", "Defintion", "Use case example", "Additional information".



          Showing all values is a little complex, so I'll explain:

          The value of "Bonjour" is this:



          {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Defintion": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }


          And all other value are described in the value of "Bonjour".

          The value of "English Word" is "Hello" and so on.



          When you write a line like so: currentword[search]['English Word', 'Definition', 'Use case example'], you are telling Python to look for a key named ('English Word', 'Definition', 'Use case example'), and obviously it does not exist.



          What you should do is as follows:



          for definition in currentword[search]:
          eng_word = definition['English Word']
          print('English Word - {}'.format(eng_word))


          please note that definition contain all other fields as well, so you can choose whichever one you like.






          share|improve this answer
























          • thanks you for helping, I wish I could give you upvote but i'm too new

            – Flexy
            Jan 19 at 9:42
















          3














          JSON format is simply a nice way to pair keys and values.
          Keys are the names we give to Values, so it will be easy to access them.

          If we took your JSON, and split it by keys and values, this is what we would get:

          Keys: "Bonjour", "English Word", "Type of word", "Defintion", "Use case example", "Additional information".



          Showing all values is a little complex, so I'll explain:

          The value of "Bonjour" is this:



          {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Defintion": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }


          And all other value are described in the value of "Bonjour".

          The value of "English Word" is "Hello" and so on.



          When you write a line like so: currentword[search]['English Word', 'Definition', 'Use case example'], you are telling Python to look for a key named ('English Word', 'Definition', 'Use case example'), and obviously it does not exist.



          What you should do is as follows:



          for definition in currentword[search]:
          eng_word = definition['English Word']
          print('English Word - {}'.format(eng_word))


          please note that definition contain all other fields as well, so you can choose whichever one you like.






          share|improve this answer
























          • thanks you for helping, I wish I could give you upvote but i'm too new

            – Flexy
            Jan 19 at 9:42














          3












          3








          3







          JSON format is simply a nice way to pair keys and values.
          Keys are the names we give to Values, so it will be easy to access them.

          If we took your JSON, and split it by keys and values, this is what we would get:

          Keys: "Bonjour", "English Word", "Type of word", "Defintion", "Use case example", "Additional information".



          Showing all values is a little complex, so I'll explain:

          The value of "Bonjour" is this:



          {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Defintion": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }


          And all other value are described in the value of "Bonjour".

          The value of "English Word" is "Hello" and so on.



          When you write a line like so: currentword[search]['English Word', 'Definition', 'Use case example'], you are telling Python to look for a key named ('English Word', 'Definition', 'Use case example'), and obviously it does not exist.



          What you should do is as follows:



          for definition in currentword[search]:
          eng_word = definition['English Word']
          print('English Word - {}'.format(eng_word))


          please note that definition contain all other fields as well, so you can choose whichever one you like.






          share|improve this answer













          JSON format is simply a nice way to pair keys and values.
          Keys are the names we give to Values, so it will be easy to access them.

          If we took your JSON, and split it by keys and values, this is what we would get:

          Keys: "Bonjour", "English Word", "Type of word", "Defintion", "Use case example", "Additional information".



          Showing all values is a little complex, so I'll explain:

          The value of "Bonjour" is this:



          {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Defintion": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }


          And all other value are described in the value of "Bonjour".

          The value of "English Word" is "Hello" and so on.



          When you write a line like so: currentword[search]['English Word', 'Definition', 'Use case example'], you are telling Python to look for a key named ('English Word', 'Definition', 'Use case example'), and obviously it does not exist.



          What you should do is as follows:



          for definition in currentword[search]:
          eng_word = definition['English Word']
          print('English Word - {}'.format(eng_word))


          please note that definition contain all other fields as well, so you can choose whichever one you like.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 9:24









          Dor ShinarDor Shinar

          318210




          318210













          • thanks you for helping, I wish I could give you upvote but i'm too new

            – Flexy
            Jan 19 at 9:42



















          • thanks you for helping, I wish I could give you upvote but i'm too new

            – Flexy
            Jan 19 at 9:42

















          thanks you for helping, I wish I could give you upvote but i'm too new

          – Flexy
          Jan 19 at 9:42





          thanks you for helping, I wish I could give you upvote but i'm too new

          – Flexy
          Jan 19 at 9:42











          1














          This line:



          currentword[search]['English Word', 'Definition', 'Use case example']


          Calls 'English Word', 'Definition', 'Use case example' as a tuple key from the inner dict, which doesn't exist in your dictionary, which is why a KeyError is raised.



          If you want just the english word, use this instead:



          currentword[search]["English Word"]


          Assuming search is "Bonjour".



          It also looks like you are also trying to filter out specific keys from the inner dict separately. If this is the case, you can do this:



          d = {
          "Bonjour": {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Defintion": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }
          }

          inner_dict = d['Bonjour']

          keys = ["English Word", "Use case example", "Defintion"]

          print({k: inner_dict[k] for k in keys})
          # {'English Word': 'Hello', 'Use case example': 'Bonjour igo', 'Defintion': 'Means good day'}





          share|improve this answer
























          • I have not verified it because the the code posted by another person worked, but still, thanks

            – Flexy
            Jan 19 at 9:50
















          1














          This line:



          currentword[search]['English Word', 'Definition', 'Use case example']


          Calls 'English Word', 'Definition', 'Use case example' as a tuple key from the inner dict, which doesn't exist in your dictionary, which is why a KeyError is raised.



          If you want just the english word, use this instead:



          currentword[search]["English Word"]


          Assuming search is "Bonjour".



          It also looks like you are also trying to filter out specific keys from the inner dict separately. If this is the case, you can do this:



          d = {
          "Bonjour": {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Defintion": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }
          }

          inner_dict = d['Bonjour']

          keys = ["English Word", "Use case example", "Defintion"]

          print({k: inner_dict[k] for k in keys})
          # {'English Word': 'Hello', 'Use case example': 'Bonjour igo', 'Defintion': 'Means good day'}





          share|improve this answer
























          • I have not verified it because the the code posted by another person worked, but still, thanks

            – Flexy
            Jan 19 at 9:50














          1












          1








          1







          This line:



          currentword[search]['English Word', 'Definition', 'Use case example']


          Calls 'English Word', 'Definition', 'Use case example' as a tuple key from the inner dict, which doesn't exist in your dictionary, which is why a KeyError is raised.



          If you want just the english word, use this instead:



          currentword[search]["English Word"]


          Assuming search is "Bonjour".



          It also looks like you are also trying to filter out specific keys from the inner dict separately. If this is the case, you can do this:



          d = {
          "Bonjour": {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Defintion": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }
          }

          inner_dict = d['Bonjour']

          keys = ["English Word", "Use case example", "Defintion"]

          print({k: inner_dict[k] for k in keys})
          # {'English Word': 'Hello', 'Use case example': 'Bonjour igo', 'Defintion': 'Means good day'}





          share|improve this answer













          This line:



          currentword[search]['English Word', 'Definition', 'Use case example']


          Calls 'English Word', 'Definition', 'Use case example' as a tuple key from the inner dict, which doesn't exist in your dictionary, which is why a KeyError is raised.



          If you want just the english word, use this instead:



          currentword[search]["English Word"]


          Assuming search is "Bonjour".



          It also looks like you are also trying to filter out specific keys from the inner dict separately. If this is the case, you can do this:



          d = {
          "Bonjour": {
          "English Word": "Hello",
          "Type of word": "whatever",
          "Defintion": "Means good day",
          "Use case example": "Bonjour igo",
          "Additional information": "BRO"
          }
          }

          inner_dict = d['Bonjour']

          keys = ["English Word", "Use case example", "Defintion"]

          print({k: inner_dict[k] for k in keys})
          # {'English Word': 'Hello', 'Use case example': 'Bonjour igo', 'Defintion': 'Means good day'}






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 9:24









          RoadRunnerRoadRunner

          11.2k31340




          11.2k31340













          • I have not verified it because the the code posted by another person worked, but still, thanks

            – Flexy
            Jan 19 at 9:50



















          • I have not verified it because the the code posted by another person worked, but still, thanks

            – Flexy
            Jan 19 at 9:50

















          I have not verified it because the the code posted by another person worked, but still, thanks

          – Flexy
          Jan 19 at 9:50





          I have not verified it because the the code posted by another person worked, but still, thanks

          – Flexy
          Jan 19 at 9:50


















          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%2f54265592%2flook-for-json-key-to-print-its-content-without-brackets-in-python%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

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage