Getting the first string from a tuple which is a key of a dict











up vote
2
down vote

favorite












I have a following python dictionary:



lines = {('mid', 'north'): None,
('south', 'mid'): None,
('south', 'north'): None,
}


what I am trying to achieve is to get the first string or/and second string of one of the keys of the lines dictionary.



so lines.keys() gives me dict_keys([('mid', 'north'), ('south', 'mid'), ('south', 'north')])



what I would like to have is something like this:



lines.keys()[0][0]
'mid'


ofc it is not working like this but is there a function or a command which gives me the first string of the tuple ('mid', 'north'), as an output? Keep in mind ('mid', 'north') is the key of the dictionary.










share|improve this question


























    up vote
    2
    down vote

    favorite












    I have a following python dictionary:



    lines = {('mid', 'north'): None,
    ('south', 'mid'): None,
    ('south', 'north'): None,
    }


    what I am trying to achieve is to get the first string or/and second string of one of the keys of the lines dictionary.



    so lines.keys() gives me dict_keys([('mid', 'north'), ('south', 'mid'), ('south', 'north')])



    what I would like to have is something like this:



    lines.keys()[0][0]
    'mid'


    ofc it is not working like this but is there a function or a command which gives me the first string of the tuple ('mid', 'north'), as an output? Keep in mind ('mid', 'north') is the key of the dictionary.










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a following python dictionary:



      lines = {('mid', 'north'): None,
      ('south', 'mid'): None,
      ('south', 'north'): None,
      }


      what I am trying to achieve is to get the first string or/and second string of one of the keys of the lines dictionary.



      so lines.keys() gives me dict_keys([('mid', 'north'), ('south', 'mid'), ('south', 'north')])



      what I would like to have is something like this:



      lines.keys()[0][0]
      'mid'


      ofc it is not working like this but is there a function or a command which gives me the first string of the tuple ('mid', 'north'), as an output? Keep in mind ('mid', 'north') is the key of the dictionary.










      share|improve this question













      I have a following python dictionary:



      lines = {('mid', 'north'): None,
      ('south', 'mid'): None,
      ('south', 'north'): None,
      }


      what I am trying to achieve is to get the first string or/and second string of one of the keys of the lines dictionary.



      so lines.keys() gives me dict_keys([('mid', 'north'), ('south', 'mid'), ('south', 'north')])



      what I would like to have is something like this:



      lines.keys()[0][0]
      'mid'


      ofc it is not working like this but is there a function or a command which gives me the first string of the tuple ('mid', 'north'), as an output? Keep in mind ('mid', 'north') is the key of the dictionary.







      python dictionary tuples key






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 12:26









      oakca

      15811




      15811
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can convert dict_keys to tuple, since it is iterable:



          >>> k = tuple(lines.keys())
          >>> k[0][0]
          'mid'





          share|improve this answer





















          • This looks like the easiest solution ty. And it works. Btw when you do k = list(lines.keys()) and then k[0][0] gives mid and not 'mid', can u explain why is that? and also add to your answer?
            – oakca
            Nov 21 at 12:37










          • k[0][0] contains the string mid. In python you would write that as 'mid' (or "mid"), but if you do print(k[0][0]) it would just output string's content (mid). If you want to obtain the string representation, do print(repr(k[0][0])), and it will print 'mid'. The interactive console gives you the string representation by default.
            – fferri
            Nov 21 at 12:39




















          up vote
          0
          down vote













          You need to convert your keys to list, and since dict will be in random order you need to sort it.



          lines = {('mid', 'north'): None,
          ('south', 'mid'): None,
          ('south', 'north'): None,
          }

          print(list(sorted(lines.keys()))[0][0]) # -> mid





          share|improve this answer























          • Looks nice, but why sorted?
            – oakca
            Nov 21 at 12:30










          • I don't think it will be in random order, but it will be the order I set them. so [0] is always ('mid', 'north'), and [1] is ('south', 'mid'), etc...
            – oakca
            Nov 21 at 12:33










          • No it won't try print(lines.keys()) running that couple of times.
            – Filip Młynarski
            Nov 21 at 12:34











          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',
          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%2f53412008%2fgetting-the-first-string-from-a-tuple-which-is-a-key-of-a-dict%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          You can convert dict_keys to tuple, since it is iterable:



          >>> k = tuple(lines.keys())
          >>> k[0][0]
          'mid'





          share|improve this answer





















          • This looks like the easiest solution ty. And it works. Btw when you do k = list(lines.keys()) and then k[0][0] gives mid and not 'mid', can u explain why is that? and also add to your answer?
            – oakca
            Nov 21 at 12:37










          • k[0][0] contains the string mid. In python you would write that as 'mid' (or "mid"), but if you do print(k[0][0]) it would just output string's content (mid). If you want to obtain the string representation, do print(repr(k[0][0])), and it will print 'mid'. The interactive console gives you the string representation by default.
            – fferri
            Nov 21 at 12:39

















          up vote
          0
          down vote



          accepted










          You can convert dict_keys to tuple, since it is iterable:



          >>> k = tuple(lines.keys())
          >>> k[0][0]
          'mid'





          share|improve this answer





















          • This looks like the easiest solution ty. And it works. Btw when you do k = list(lines.keys()) and then k[0][0] gives mid and not 'mid', can u explain why is that? and also add to your answer?
            – oakca
            Nov 21 at 12:37










          • k[0][0] contains the string mid. In python you would write that as 'mid' (or "mid"), but if you do print(k[0][0]) it would just output string's content (mid). If you want to obtain the string representation, do print(repr(k[0][0])), and it will print 'mid'. The interactive console gives you the string representation by default.
            – fferri
            Nov 21 at 12:39















          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You can convert dict_keys to tuple, since it is iterable:



          >>> k = tuple(lines.keys())
          >>> k[0][0]
          'mid'





          share|improve this answer












          You can convert dict_keys to tuple, since it is iterable:



          >>> k = tuple(lines.keys())
          >>> k[0][0]
          'mid'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 12:33









          fferri

          11.5k22251




          11.5k22251












          • This looks like the easiest solution ty. And it works. Btw when you do k = list(lines.keys()) and then k[0][0] gives mid and not 'mid', can u explain why is that? and also add to your answer?
            – oakca
            Nov 21 at 12:37










          • k[0][0] contains the string mid. In python you would write that as 'mid' (or "mid"), but if you do print(k[0][0]) it would just output string's content (mid). If you want to obtain the string representation, do print(repr(k[0][0])), and it will print 'mid'. The interactive console gives you the string representation by default.
            – fferri
            Nov 21 at 12:39




















          • This looks like the easiest solution ty. And it works. Btw when you do k = list(lines.keys()) and then k[0][0] gives mid and not 'mid', can u explain why is that? and also add to your answer?
            – oakca
            Nov 21 at 12:37










          • k[0][0] contains the string mid. In python you would write that as 'mid' (or "mid"), but if you do print(k[0][0]) it would just output string's content (mid). If you want to obtain the string representation, do print(repr(k[0][0])), and it will print 'mid'. The interactive console gives you the string representation by default.
            – fferri
            Nov 21 at 12:39


















          This looks like the easiest solution ty. And it works. Btw when you do k = list(lines.keys()) and then k[0][0] gives mid and not 'mid', can u explain why is that? and also add to your answer?
          – oakca
          Nov 21 at 12:37




          This looks like the easiest solution ty. And it works. Btw when you do k = list(lines.keys()) and then k[0][0] gives mid and not 'mid', can u explain why is that? and also add to your answer?
          – oakca
          Nov 21 at 12:37












          k[0][0] contains the string mid. In python you would write that as 'mid' (or "mid"), but if you do print(k[0][0]) it would just output string's content (mid). If you want to obtain the string representation, do print(repr(k[0][0])), and it will print 'mid'. The interactive console gives you the string representation by default.
          – fferri
          Nov 21 at 12:39






          k[0][0] contains the string mid. In python you would write that as 'mid' (or "mid"), but if you do print(k[0][0]) it would just output string's content (mid). If you want to obtain the string representation, do print(repr(k[0][0])), and it will print 'mid'. The interactive console gives you the string representation by default.
          – fferri
          Nov 21 at 12:39














          up vote
          0
          down vote













          You need to convert your keys to list, and since dict will be in random order you need to sort it.



          lines = {('mid', 'north'): None,
          ('south', 'mid'): None,
          ('south', 'north'): None,
          }

          print(list(sorted(lines.keys()))[0][0]) # -> mid





          share|improve this answer























          • Looks nice, but why sorted?
            – oakca
            Nov 21 at 12:30










          • I don't think it will be in random order, but it will be the order I set them. so [0] is always ('mid', 'north'), and [1] is ('south', 'mid'), etc...
            – oakca
            Nov 21 at 12:33










          • No it won't try print(lines.keys()) running that couple of times.
            – Filip Młynarski
            Nov 21 at 12:34















          up vote
          0
          down vote













          You need to convert your keys to list, and since dict will be in random order you need to sort it.



          lines = {('mid', 'north'): None,
          ('south', 'mid'): None,
          ('south', 'north'): None,
          }

          print(list(sorted(lines.keys()))[0][0]) # -> mid





          share|improve this answer























          • Looks nice, but why sorted?
            – oakca
            Nov 21 at 12:30










          • I don't think it will be in random order, but it will be the order I set them. so [0] is always ('mid', 'north'), and [1] is ('south', 'mid'), etc...
            – oakca
            Nov 21 at 12:33










          • No it won't try print(lines.keys()) running that couple of times.
            – Filip Młynarski
            Nov 21 at 12:34













          up vote
          0
          down vote










          up vote
          0
          down vote









          You need to convert your keys to list, and since dict will be in random order you need to sort it.



          lines = {('mid', 'north'): None,
          ('south', 'mid'): None,
          ('south', 'north'): None,
          }

          print(list(sorted(lines.keys()))[0][0]) # -> mid





          share|improve this answer














          You need to convert your keys to list, and since dict will be in random order you need to sort it.



          lines = {('mid', 'north'): None,
          ('south', 'mid'): None,
          ('south', 'north'): None,
          }

          print(list(sorted(lines.keys()))[0][0]) # -> mid






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 at 12:30

























          answered Nov 21 at 12:29









          Filip Młynarski

          1,351111




          1,351111












          • Looks nice, but why sorted?
            – oakca
            Nov 21 at 12:30










          • I don't think it will be in random order, but it will be the order I set them. so [0] is always ('mid', 'north'), and [1] is ('south', 'mid'), etc...
            – oakca
            Nov 21 at 12:33










          • No it won't try print(lines.keys()) running that couple of times.
            – Filip Młynarski
            Nov 21 at 12:34


















          • Looks nice, but why sorted?
            – oakca
            Nov 21 at 12:30










          • I don't think it will be in random order, but it will be the order I set them. so [0] is always ('mid', 'north'), and [1] is ('south', 'mid'), etc...
            – oakca
            Nov 21 at 12:33










          • No it won't try print(lines.keys()) running that couple of times.
            – Filip Młynarski
            Nov 21 at 12:34
















          Looks nice, but why sorted?
          – oakca
          Nov 21 at 12:30




          Looks nice, but why sorted?
          – oakca
          Nov 21 at 12:30












          I don't think it will be in random order, but it will be the order I set them. so [0] is always ('mid', 'north'), and [1] is ('south', 'mid'), etc...
          – oakca
          Nov 21 at 12:33




          I don't think it will be in random order, but it will be the order I set them. so [0] is always ('mid', 'north'), and [1] is ('south', 'mid'), etc...
          – oakca
          Nov 21 at 12:33












          No it won't try print(lines.keys()) running that couple of times.
          – Filip Młynarski
          Nov 21 at 12:34




          No it won't try print(lines.keys()) running that couple of times.
          – Filip Młynarski
          Nov 21 at 12:34


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53412008%2fgetting-the-first-string-from-a-tuple-which-is-a-key-of-a-dict%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

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...