Function that adds two items from a list in search of those that will match some given sum











up vote
0
down vote

favorite












I'm doing a Daily Challenges to get better with Python.
I completed the first challenges with a working code but I want to make sure if there's anyway to improve or optimize it.



I have a function that adds 2 items from a list in search of those that will match the already defined result.



def main():

listA = [10,11,3,7]
k = 21

for i in listA:
for e in listA:
result = i + e
if result == k:
print(i, '+', e, ' = ', k)
return
main()


I was thinking of reducing it to:




def main():
k = 21
listA=[10,11,3,7]
print (k,'=',[(i and e) for i in listA for e in listA if k==(i+e)])
main()



… but that would print 21 = [10,11] instead of 21 = 10 + 11.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm doing a Daily Challenges to get better with Python.
    I completed the first challenges with a working code but I want to make sure if there's anyway to improve or optimize it.



    I have a function that adds 2 items from a list in search of those that will match the already defined result.



    def main():

    listA = [10,11,3,7]
    k = 21

    for i in listA:
    for e in listA:
    result = i + e
    if result == k:
    print(i, '+', e, ' = ', k)
    return
    main()


    I was thinking of reducing it to:




    def main():
    k = 21
    listA=[10,11,3,7]
    print (k,'=',[(i and e) for i in listA for e in listA if k==(i+e)])
    main()



    … but that would print 21 = [10,11] instead of 21 = 10 + 11.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm doing a Daily Challenges to get better with Python.
      I completed the first challenges with a working code but I want to make sure if there's anyway to improve or optimize it.



      I have a function that adds 2 items from a list in search of those that will match the already defined result.



      def main():

      listA = [10,11,3,7]
      k = 21

      for i in listA:
      for e in listA:
      result = i + e
      if result == k:
      print(i, '+', e, ' = ', k)
      return
      main()


      I was thinking of reducing it to:




      def main():
      k = 21
      listA=[10,11,3,7]
      print (k,'=',[(i and e) for i in listA for e in listA if k==(i+e)])
      main()



      … but that would print 21 = [10,11] instead of 21 = 10 + 11.










      share|improve this question















      I'm doing a Daily Challenges to get better with Python.
      I completed the first challenges with a working code but I want to make sure if there's anyway to improve or optimize it.



      I have a function that adds 2 items from a list in search of those that will match the already defined result.



      def main():

      listA = [10,11,3,7]
      k = 21

      for i in listA:
      for e in listA:
      result = i + e
      if result == k:
      print(i, '+', e, ' = ', k)
      return
      main()


      I was thinking of reducing it to:




      def main():
      k = 21
      listA=[10,11,3,7]
      print (k,'=',[(i and e) for i in listA for e in listA if k==(i+e)])
      main()



      … but that would print 21 = [10,11] instead of 21 = 10 + 11.







      python python-3.x programming-challenge






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 at 0:04









      Gerrit0

      2,9191522




      2,9191522










      asked Nov 26 at 19:58









      Mark-André Tremblay

      32




      32






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          def main():
          k = 21
          listA=[10,11,3,7]
          print (k,' = '," + ".join([str(i and e) for i in listA for e in listA if k==(i+e)]))

          main()


          This minor change to your code will do what you're after. Put a " + ".join( ) around your list creator and cast the (i and e) items to str, that will then join all string elements in the list together into a string with + as the separator (there are spaces in there, yes, and this is necessary to get the output you're after).



          Proof of Concept (via tio.run) that shows you how this will execute.



          Note I also made a change to add spaces around the equals sign, indentation, and extra spacing for readability, as well as indentation standardization (4 spaces for a single indentation level).






          share|improve this answer























          • Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
            – Mark-André Tremblay
            Nov 26 at 21:59










          • @Mark-AndréTremblay it's a string function, a builtin as part of the str datatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
            – Thomas Ward
            Nov 27 at 1:35













          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f208471%2ffunction-that-adds-two-items-from-a-list-in-search-of-those-that-will-match-some%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








          up vote
          1
          down vote



          accepted










          def main():
          k = 21
          listA=[10,11,3,7]
          print (k,' = '," + ".join([str(i and e) for i in listA for e in listA if k==(i+e)]))

          main()


          This minor change to your code will do what you're after. Put a " + ".join( ) around your list creator and cast the (i and e) items to str, that will then join all string elements in the list together into a string with + as the separator (there are spaces in there, yes, and this is necessary to get the output you're after).



          Proof of Concept (via tio.run) that shows you how this will execute.



          Note I also made a change to add spaces around the equals sign, indentation, and extra spacing for readability, as well as indentation standardization (4 spaces for a single indentation level).






          share|improve this answer























          • Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
            – Mark-André Tremblay
            Nov 26 at 21:59










          • @Mark-AndréTremblay it's a string function, a builtin as part of the str datatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
            – Thomas Ward
            Nov 27 at 1:35

















          up vote
          1
          down vote



          accepted










          def main():
          k = 21
          listA=[10,11,3,7]
          print (k,' = '," + ".join([str(i and e) for i in listA for e in listA if k==(i+e)]))

          main()


          This minor change to your code will do what you're after. Put a " + ".join( ) around your list creator and cast the (i and e) items to str, that will then join all string elements in the list together into a string with + as the separator (there are spaces in there, yes, and this is necessary to get the output you're after).



          Proof of Concept (via tio.run) that shows you how this will execute.



          Note I also made a change to add spaces around the equals sign, indentation, and extra spacing for readability, as well as indentation standardization (4 spaces for a single indentation level).






          share|improve this answer























          • Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
            – Mark-André Tremblay
            Nov 26 at 21:59










          • @Mark-AndréTremblay it's a string function, a builtin as part of the str datatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
            – Thomas Ward
            Nov 27 at 1:35















          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          def main():
          k = 21
          listA=[10,11,3,7]
          print (k,' = '," + ".join([str(i and e) for i in listA for e in listA if k==(i+e)]))

          main()


          This minor change to your code will do what you're after. Put a " + ".join( ) around your list creator and cast the (i and e) items to str, that will then join all string elements in the list together into a string with + as the separator (there are spaces in there, yes, and this is necessary to get the output you're after).



          Proof of Concept (via tio.run) that shows you how this will execute.



          Note I also made a change to add spaces around the equals sign, indentation, and extra spacing for readability, as well as indentation standardization (4 spaces for a single indentation level).






          share|improve this answer














          def main():
          k = 21
          listA=[10,11,3,7]
          print (k,' = '," + ".join([str(i and e) for i in listA for e in listA if k==(i+e)]))

          main()


          This minor change to your code will do what you're after. Put a " + ".join( ) around your list creator and cast the (i and e) items to str, that will then join all string elements in the list together into a string with + as the separator (there are spaces in there, yes, and this is necessary to get the output you're after).



          Proof of Concept (via tio.run) that shows you how this will execute.



          Note I also made a change to add spaces around the equals sign, indentation, and extra spacing for readability, as well as indentation standardization (4 spaces for a single indentation level).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 27 at 1:37

























          answered Nov 26 at 20:55









          Thomas Ward

          1,773823




          1,773823












          • Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
            – Mark-André Tremblay
            Nov 26 at 21:59










          • @Mark-AndréTremblay it's a string function, a builtin as part of the str datatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
            – Thomas Ward
            Nov 27 at 1:35




















          • Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
            – Mark-André Tremblay
            Nov 26 at 21:59










          • @Mark-AndréTremblay it's a string function, a builtin as part of the str datatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
            – Thomas Ward
            Nov 27 at 1:35


















          Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
          – Mark-André Tremblay
          Nov 26 at 21:59




          Thanks, It worked perfectly. the .join() is that a built-in function in Python? Like append?
          – Mark-André Tremblay
          Nov 26 at 21:59












          @Mark-AndréTremblay it's a string function, a builtin as part of the str datatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
          – Thomas Ward
          Nov 27 at 1:35






          @Mark-AndréTremblay it's a string function, a builtin as part of the str datatype. (Python documentation for 3.7 on str.join is better probably for answering your question)
          – Thomas Ward
          Nov 27 at 1:35




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


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


          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f208471%2ffunction-that-adds-two-items-from-a-list-in-search-of-those-that-will-match-some%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

          Terni

          A new problem with tex4ht and tikz

          Sun Ra