why can't I print a single tuple from a list?












2














I'm just trying to flip and print the first tuple in a list.
If I try this code I get error "cannot unpack non-iterable int object"



lst = [('a',1),('b',2),('c',3)]
for x,y in lst[0]:
print(y,x)


However if I make this simple edit, it works fine. why can't I print a single tuple from a list?



lst = [('a',1),('b',2),('c',3)]
for x,y in lst[:1]:
print(y,x)









share|improve this question




















  • 3




    Why are you iterating?
    – user2357112
    Nov 23 '18 at 18:03






  • 4




    Because in lst[0] you index list by integer which returns single element however in lst[:1] you index list by slice which returns list that you can iterate over.
    – Filip Młynarski
    Nov 23 '18 at 18:04






  • 1




    print(lst[0][1], lst[0][0]).
    – Austin
    Nov 23 '18 at 18:05
















2














I'm just trying to flip and print the first tuple in a list.
If I try this code I get error "cannot unpack non-iterable int object"



lst = [('a',1),('b',2),('c',3)]
for x,y in lst[0]:
print(y,x)


However if I make this simple edit, it works fine. why can't I print a single tuple from a list?



lst = [('a',1),('b',2),('c',3)]
for x,y in lst[:1]:
print(y,x)









share|improve this question




















  • 3




    Why are you iterating?
    – user2357112
    Nov 23 '18 at 18:03






  • 4




    Because in lst[0] you index list by integer which returns single element however in lst[:1] you index list by slice which returns list that you can iterate over.
    – Filip Młynarski
    Nov 23 '18 at 18:04






  • 1




    print(lst[0][1], lst[0][0]).
    – Austin
    Nov 23 '18 at 18:05














2












2








2







I'm just trying to flip and print the first tuple in a list.
If I try this code I get error "cannot unpack non-iterable int object"



lst = [('a',1),('b',2),('c',3)]
for x,y in lst[0]:
print(y,x)


However if I make this simple edit, it works fine. why can't I print a single tuple from a list?



lst = [('a',1),('b',2),('c',3)]
for x,y in lst[:1]:
print(y,x)









share|improve this question















I'm just trying to flip and print the first tuple in a list.
If I try this code I get error "cannot unpack non-iterable int object"



lst = [('a',1),('b',2),('c',3)]
for x,y in lst[0]:
print(y,x)


However if I make this simple edit, it works fine. why can't I print a single tuple from a list?



lst = [('a',1),('b',2),('c',3)]
for x,y in lst[:1]:
print(y,x)






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 18:05









slider

8,08011129




8,08011129










asked Nov 23 '18 at 18:02









xgeniux

245




245








  • 3




    Why are you iterating?
    – user2357112
    Nov 23 '18 at 18:03






  • 4




    Because in lst[0] you index list by integer which returns single element however in lst[:1] you index list by slice which returns list that you can iterate over.
    – Filip Młynarski
    Nov 23 '18 at 18:04






  • 1




    print(lst[0][1], lst[0][0]).
    – Austin
    Nov 23 '18 at 18:05














  • 3




    Why are you iterating?
    – user2357112
    Nov 23 '18 at 18:03






  • 4




    Because in lst[0] you index list by integer which returns single element however in lst[:1] you index list by slice which returns list that you can iterate over.
    – Filip Młynarski
    Nov 23 '18 at 18:04






  • 1




    print(lst[0][1], lst[0][0]).
    – Austin
    Nov 23 '18 at 18:05








3




3




Why are you iterating?
– user2357112
Nov 23 '18 at 18:03




Why are you iterating?
– user2357112
Nov 23 '18 at 18:03




4




4




Because in lst[0] you index list by integer which returns single element however in lst[:1] you index list by slice which returns list that you can iterate over.
– Filip Młynarski
Nov 23 '18 at 18:04




Because in lst[0] you index list by integer which returns single element however in lst[:1] you index list by slice which returns list that you can iterate over.
– Filip Młynarski
Nov 23 '18 at 18:04




1




1




print(lst[0][1], lst[0][0]).
– Austin
Nov 23 '18 at 18:05




print(lst[0][1], lst[0][0]).
– Austin
Nov 23 '18 at 18:05












4 Answers
4






active

oldest

votes


















3














Easiest way to flip first element would be to:



lst = [('a',1),('b',2),('c',3)]
print((lst[0][1], lst[0][0])) # -> (1, 'a')


However reason why your code throws error here is because you try to iterate over element instead of list, where element is ('a',1) by iterating over it first value is 'a' and you try to split it into 2 variables x and y which throws an error.



print(type(lst[0])) # -> <class 'tuple'>
for x,y in lst[0]:
print(y,x)


Here if we iterate over list [('a',1)] (because we used slice as list index instead of integer) first value is indeed ('a',1) and we can split it into x and y variables without errors.



print(type(lst[:1])) # -> <class 'list'>
for x,y in lst[:1]:
print(y,x)





share|improve this answer





























    4














    Your list:



    lst = [('a',1),('b',2),('c',3)]


    The first element of the list:



    >>> lst[0]
    (1, 'a')


    Then when you iterate over this, you are asking to unpack each element. It would be like writing



    for x, y in 1:
    # do something
    for x, y in 'a':
    # do something


    Wth lst[:1], you are slicing the list, and getting a list of tuples back.






    share|improve this answer





























      1














      You are trying to loop through the first element of the list.
      Just unzip the first element and print:



      x, y = lst[0]
      print(y,x)


      or in a loop:



      for x, y in lst:
      print(y, x)





      share|improve this answer































        0














        You don't want a loop there. lst[0] is the tuple you want to flip, there is no need to iterate.



        x, y = lst[0]
        print(y, X)





        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%2f53451194%2fwhy-cant-i-print-a-single-tuple-from-a-list%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Easiest way to flip first element would be to:



          lst = [('a',1),('b',2),('c',3)]
          print((lst[0][1], lst[0][0])) # -> (1, 'a')


          However reason why your code throws error here is because you try to iterate over element instead of list, where element is ('a',1) by iterating over it first value is 'a' and you try to split it into 2 variables x and y which throws an error.



          print(type(lst[0])) # -> <class 'tuple'>
          for x,y in lst[0]:
          print(y,x)


          Here if we iterate over list [('a',1)] (because we used slice as list index instead of integer) first value is indeed ('a',1) and we can split it into x and y variables without errors.



          print(type(lst[:1])) # -> <class 'list'>
          for x,y in lst[:1]:
          print(y,x)





          share|improve this answer


























            3














            Easiest way to flip first element would be to:



            lst = [('a',1),('b',2),('c',3)]
            print((lst[0][1], lst[0][0])) # -> (1, 'a')


            However reason why your code throws error here is because you try to iterate over element instead of list, where element is ('a',1) by iterating over it first value is 'a' and you try to split it into 2 variables x and y which throws an error.



            print(type(lst[0])) # -> <class 'tuple'>
            for x,y in lst[0]:
            print(y,x)


            Here if we iterate over list [('a',1)] (because we used slice as list index instead of integer) first value is indeed ('a',1) and we can split it into x and y variables without errors.



            print(type(lst[:1])) # -> <class 'list'>
            for x,y in lst[:1]:
            print(y,x)





            share|improve this answer
























              3












              3








              3






              Easiest way to flip first element would be to:



              lst = [('a',1),('b',2),('c',3)]
              print((lst[0][1], lst[0][0])) # -> (1, 'a')


              However reason why your code throws error here is because you try to iterate over element instead of list, where element is ('a',1) by iterating over it first value is 'a' and you try to split it into 2 variables x and y which throws an error.



              print(type(lst[0])) # -> <class 'tuple'>
              for x,y in lst[0]:
              print(y,x)


              Here if we iterate over list [('a',1)] (because we used slice as list index instead of integer) first value is indeed ('a',1) and we can split it into x and y variables without errors.



              print(type(lst[:1])) # -> <class 'list'>
              for x,y in lst[:1]:
              print(y,x)





              share|improve this answer












              Easiest way to flip first element would be to:



              lst = [('a',1),('b',2),('c',3)]
              print((lst[0][1], lst[0][0])) # -> (1, 'a')


              However reason why your code throws error here is because you try to iterate over element instead of list, where element is ('a',1) by iterating over it first value is 'a' and you try to split it into 2 variables x and y which throws an error.



              print(type(lst[0])) # -> <class 'tuple'>
              for x,y in lst[0]:
              print(y,x)


              Here if we iterate over list [('a',1)] (because we used slice as list index instead of integer) first value is indeed ('a',1) and we can split it into x and y variables without errors.



              print(type(lst[:1])) # -> <class 'list'>
              for x,y in lst[:1]:
              print(y,x)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '18 at 18:15









              Filip Młynarski

              1,5931311




              1,5931311

























                  4














                  Your list:



                  lst = [('a',1),('b',2),('c',3)]


                  The first element of the list:



                  >>> lst[0]
                  (1, 'a')


                  Then when you iterate over this, you are asking to unpack each element. It would be like writing



                  for x, y in 1:
                  # do something
                  for x, y in 'a':
                  # do something


                  Wth lst[:1], you are slicing the list, and getting a list of tuples back.






                  share|improve this answer


























                    4














                    Your list:



                    lst = [('a',1),('b',2),('c',3)]


                    The first element of the list:



                    >>> lst[0]
                    (1, 'a')


                    Then when you iterate over this, you are asking to unpack each element. It would be like writing



                    for x, y in 1:
                    # do something
                    for x, y in 'a':
                    # do something


                    Wth lst[:1], you are slicing the list, and getting a list of tuples back.






                    share|improve this answer
























                      4












                      4








                      4






                      Your list:



                      lst = [('a',1),('b',2),('c',3)]


                      The first element of the list:



                      >>> lst[0]
                      (1, 'a')


                      Then when you iterate over this, you are asking to unpack each element. It would be like writing



                      for x, y in 1:
                      # do something
                      for x, y in 'a':
                      # do something


                      Wth lst[:1], you are slicing the list, and getting a list of tuples back.






                      share|improve this answer












                      Your list:



                      lst = [('a',1),('b',2),('c',3)]


                      The first element of the list:



                      >>> lst[0]
                      (1, 'a')


                      Then when you iterate over this, you are asking to unpack each element. It would be like writing



                      for x, y in 1:
                      # do something
                      for x, y in 'a':
                      # do something


                      Wth lst[:1], you are slicing the list, and getting a list of tuples back.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 23 '18 at 18:05









                      JETM

                      1,89241627




                      1,89241627























                          1














                          You are trying to loop through the first element of the list.
                          Just unzip the first element and print:



                          x, y = lst[0]
                          print(y,x)


                          or in a loop:



                          for x, y in lst:
                          print(y, x)





                          share|improve this answer




























                            1














                            You are trying to loop through the first element of the list.
                            Just unzip the first element and print:



                            x, y = lst[0]
                            print(y,x)


                            or in a loop:



                            for x, y in lst:
                            print(y, x)





                            share|improve this answer


























                              1












                              1








                              1






                              You are trying to loop through the first element of the list.
                              Just unzip the first element and print:



                              x, y = lst[0]
                              print(y,x)


                              or in a loop:



                              for x, y in lst:
                              print(y, x)





                              share|improve this answer














                              You are trying to loop through the first element of the list.
                              Just unzip the first element and print:



                              x, y = lst[0]
                              print(y,x)


                              or in a loop:



                              for x, y in lst:
                              print(y, x)






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 23 '18 at 18:21









                              handras

                              410115




                              410115










                              answered Nov 23 '18 at 18:05









                              Attila Bognár

                              1226




                              1226























                                  0














                                  You don't want a loop there. lst[0] is the tuple you want to flip, there is no need to iterate.



                                  x, y = lst[0]
                                  print(y, X)





                                  share|improve this answer


























                                    0














                                    You don't want a loop there. lst[0] is the tuple you want to flip, there is no need to iterate.



                                    x, y = lst[0]
                                    print(y, X)





                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      You don't want a loop there. lst[0] is the tuple you want to flip, there is no need to iterate.



                                      x, y = lst[0]
                                      print(y, X)





                                      share|improve this answer












                                      You don't want a loop there. lst[0] is the tuple you want to flip, there is no need to iterate.



                                      x, y = lst[0]
                                      print(y, X)






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 23 '18 at 18:05









                                      Daniel Roseman

                                      444k41576632




                                      444k41576632






























                                          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%2f53451194%2fwhy-cant-i-print-a-single-tuple-from-a-list%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

                                          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                          Calculate evaluation metrics using cross_val_predict sklearn

                                          Insert data from modal to MySQL (multiple modal on website)