Displaying a sentence letter by letter including spaces











up vote
1
down vote

favorite












Can I make this simpler anyhow?



My code displays it correctly:



v = lambda x: [x[0:i+1] for i in range(len(x))]+[x[0:-i-1] for i in range(len(x))]  
print(*v(input("Enter a sentence: ")), sep="n")


This also works:





text = input("Enter a sentence: ")
v = [text[:i] for i in range(1, len(text)+1)]
print(*(v + v[::-1]), sep='n')


This is what it prints:



Enter something: test
t
te
tes
test
tes
te
t









share|improve this question









New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Csn you just suggest an easier version of the first one
    – Samardeep Khurana
    9 hours ago






  • 2




    Have you written this yourself and do you fully understand how it works?
    – janos
    8 hours ago






  • 1




    not exactly to be honest
    – Samardeep Khurana
    8 hours ago















up vote
1
down vote

favorite












Can I make this simpler anyhow?



My code displays it correctly:



v = lambda x: [x[0:i+1] for i in range(len(x))]+[x[0:-i-1] for i in range(len(x))]  
print(*v(input("Enter a sentence: ")), sep="n")


This also works:





text = input("Enter a sentence: ")
v = [text[:i] for i in range(1, len(text)+1)]
print(*(v + v[::-1]), sep='n')


This is what it prints:



Enter something: test
t
te
tes
test
tes
te
t









share|improve this question









New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Csn you just suggest an easier version of the first one
    – Samardeep Khurana
    9 hours ago






  • 2




    Have you written this yourself and do you fully understand how it works?
    – janos
    8 hours ago






  • 1




    not exactly to be honest
    – Samardeep Khurana
    8 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Can I make this simpler anyhow?



My code displays it correctly:



v = lambda x: [x[0:i+1] for i in range(len(x))]+[x[0:-i-1] for i in range(len(x))]  
print(*v(input("Enter a sentence: ")), sep="n")


This also works:





text = input("Enter a sentence: ")
v = [text[:i] for i in range(1, len(text)+1)]
print(*(v + v[::-1]), sep='n')


This is what it prints:



Enter something: test
t
te
tes
test
tes
te
t









share|improve this question









New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Can I make this simpler anyhow?



My code displays it correctly:



v = lambda x: [x[0:i+1] for i in range(len(x))]+[x[0:-i-1] for i in range(len(x))]  
print(*v(input("Enter a sentence: ")), sep="n")


This also works:





text = input("Enter a sentence: ")
v = [text[:i] for i in range(1, len(text)+1)]
print(*(v + v[::-1]), sep='n')


This is what it prints:



Enter something: test
t
te
tes
test
tes
te
t






python python-3.x






share|improve this question









New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 mins ago









Jamal

30.2k11115226




30.2k11115226






New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 10 hours ago









Samardeep Khurana

92




92




New contributor




Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Samardeep Khurana is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Csn you just suggest an easier version of the first one
    – Samardeep Khurana
    9 hours ago






  • 2




    Have you written this yourself and do you fully understand how it works?
    – janos
    8 hours ago






  • 1




    not exactly to be honest
    – Samardeep Khurana
    8 hours ago


















  • Csn you just suggest an easier version of the first one
    – Samardeep Khurana
    9 hours ago






  • 2




    Have you written this yourself and do you fully understand how it works?
    – janos
    8 hours ago






  • 1




    not exactly to be honest
    – Samardeep Khurana
    8 hours ago
















Csn you just suggest an easier version of the first one
– Samardeep Khurana
9 hours ago




Csn you just suggest an easier version of the first one
– Samardeep Khurana
9 hours ago




2




2




Have you written this yourself and do you fully understand how it works?
– janos
8 hours ago




Have you written this yourself and do you fully understand how it works?
– janos
8 hours ago




1




1




not exactly to be honest
– Samardeep Khurana
8 hours ago




not exactly to be honest
– Samardeep Khurana
8 hours ago










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Since the program is only a snippet, there's not much to talk about in terms of larger design choices, but there are a few things of note:





  • Inconsistency / non-identical programs: Your two programs are not identical: the first one produces the original word once, while the second version produces the ascending list and then the same list reversed; hence, the original word appears twice. This could be fixed by tweaking the first list to remove the last element, like v[:-1]. You can read more about list slices here; they're generally a useful tool.


  • Efficiency: Unless having the list for a later part of the program is important, it can be made more efficient by using iterators. Iterators can output a pattern's elements one-by-one without storing the entire pattern in memory. For a small program like this, it doesn't make any difference, but being aware of the technique is useful as the situations scale up. This SO answer explains the rationale of using iterators in a bit more detail. Using iterators, one could write:



    import itertools as it
    intext = input("Enter a sentence: ")
    for s in it.chain((intext[:i] for i in range(1, len(intext))),
    (intext[:i] for i in range(len(intext), 0, -1))):
    print(s)


    Here, itertools (an extremely useful standard library, probably one of the most commonly used standard libraries), is used to chain two two iterators together so they are iterated one after another using the nicely named chain function. The mid-line indent is used to limit the line length, and complies with PEP 8's guidelines about indenting split lines.



  • Other critiques: Concatenating two lists together is generally something to avoid if you can, because it's somewhat inefficient. Lambda functions should probably be used sparingly unless they offer a utility over functions in your particular situation. Unpacking argument lists is clever, but in this situation, it's probably more clear and efficient to just iterate through a for-loop as shown above.





If you want to learn more Python, I highly recommend starting to read through portions of the Python docs as they become relevant. They're generally very well-written and easy to understand. Reading Stack Overflow questions to find solutions to specific programming problems also helps, but the docs help fill in the gaps.






share|improve this answer























    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
    });


    }
    });






    Samardeep Khurana is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209851%2fdisplaying-a-sentence-letter-by-letter-including-spaces%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
    0
    down vote













    Since the program is only a snippet, there's not much to talk about in terms of larger design choices, but there are a few things of note:





    • Inconsistency / non-identical programs: Your two programs are not identical: the first one produces the original word once, while the second version produces the ascending list and then the same list reversed; hence, the original word appears twice. This could be fixed by tweaking the first list to remove the last element, like v[:-1]. You can read more about list slices here; they're generally a useful tool.


    • Efficiency: Unless having the list for a later part of the program is important, it can be made more efficient by using iterators. Iterators can output a pattern's elements one-by-one without storing the entire pattern in memory. For a small program like this, it doesn't make any difference, but being aware of the technique is useful as the situations scale up. This SO answer explains the rationale of using iterators in a bit more detail. Using iterators, one could write:



      import itertools as it
      intext = input("Enter a sentence: ")
      for s in it.chain((intext[:i] for i in range(1, len(intext))),
      (intext[:i] for i in range(len(intext), 0, -1))):
      print(s)


      Here, itertools (an extremely useful standard library, probably one of the most commonly used standard libraries), is used to chain two two iterators together so they are iterated one after another using the nicely named chain function. The mid-line indent is used to limit the line length, and complies with PEP 8's guidelines about indenting split lines.



    • Other critiques: Concatenating two lists together is generally something to avoid if you can, because it's somewhat inefficient. Lambda functions should probably be used sparingly unless they offer a utility over functions in your particular situation. Unpacking argument lists is clever, but in this situation, it's probably more clear and efficient to just iterate through a for-loop as shown above.





    If you want to learn more Python, I highly recommend starting to read through portions of the Python docs as they become relevant. They're generally very well-written and easy to understand. Reading Stack Overflow questions to find solutions to specific programming problems also helps, but the docs help fill in the gaps.






    share|improve this answer



























      up vote
      0
      down vote













      Since the program is only a snippet, there's not much to talk about in terms of larger design choices, but there are a few things of note:





      • Inconsistency / non-identical programs: Your two programs are not identical: the first one produces the original word once, while the second version produces the ascending list and then the same list reversed; hence, the original word appears twice. This could be fixed by tweaking the first list to remove the last element, like v[:-1]. You can read more about list slices here; they're generally a useful tool.


      • Efficiency: Unless having the list for a later part of the program is important, it can be made more efficient by using iterators. Iterators can output a pattern's elements one-by-one without storing the entire pattern in memory. For a small program like this, it doesn't make any difference, but being aware of the technique is useful as the situations scale up. This SO answer explains the rationale of using iterators in a bit more detail. Using iterators, one could write:



        import itertools as it
        intext = input("Enter a sentence: ")
        for s in it.chain((intext[:i] for i in range(1, len(intext))),
        (intext[:i] for i in range(len(intext), 0, -1))):
        print(s)


        Here, itertools (an extremely useful standard library, probably one of the most commonly used standard libraries), is used to chain two two iterators together so they are iterated one after another using the nicely named chain function. The mid-line indent is used to limit the line length, and complies with PEP 8's guidelines about indenting split lines.



      • Other critiques: Concatenating two lists together is generally something to avoid if you can, because it's somewhat inefficient. Lambda functions should probably be used sparingly unless they offer a utility over functions in your particular situation. Unpacking argument lists is clever, but in this situation, it's probably more clear and efficient to just iterate through a for-loop as shown above.





      If you want to learn more Python, I highly recommend starting to read through portions of the Python docs as they become relevant. They're generally very well-written and easy to understand. Reading Stack Overflow questions to find solutions to specific programming problems also helps, but the docs help fill in the gaps.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Since the program is only a snippet, there's not much to talk about in terms of larger design choices, but there are a few things of note:





        • Inconsistency / non-identical programs: Your two programs are not identical: the first one produces the original word once, while the second version produces the ascending list and then the same list reversed; hence, the original word appears twice. This could be fixed by tweaking the first list to remove the last element, like v[:-1]. You can read more about list slices here; they're generally a useful tool.


        • Efficiency: Unless having the list for a later part of the program is important, it can be made more efficient by using iterators. Iterators can output a pattern's elements one-by-one without storing the entire pattern in memory. For a small program like this, it doesn't make any difference, but being aware of the technique is useful as the situations scale up. This SO answer explains the rationale of using iterators in a bit more detail. Using iterators, one could write:



          import itertools as it
          intext = input("Enter a sentence: ")
          for s in it.chain((intext[:i] for i in range(1, len(intext))),
          (intext[:i] for i in range(len(intext), 0, -1))):
          print(s)


          Here, itertools (an extremely useful standard library, probably one of the most commonly used standard libraries), is used to chain two two iterators together so they are iterated one after another using the nicely named chain function. The mid-line indent is used to limit the line length, and complies with PEP 8's guidelines about indenting split lines.



        • Other critiques: Concatenating two lists together is generally something to avoid if you can, because it's somewhat inefficient. Lambda functions should probably be used sparingly unless they offer a utility over functions in your particular situation. Unpacking argument lists is clever, but in this situation, it's probably more clear and efficient to just iterate through a for-loop as shown above.





        If you want to learn more Python, I highly recommend starting to read through portions of the Python docs as they become relevant. They're generally very well-written and easy to understand. Reading Stack Overflow questions to find solutions to specific programming problems also helps, but the docs help fill in the gaps.






        share|improve this answer














        Since the program is only a snippet, there's not much to talk about in terms of larger design choices, but there are a few things of note:





        • Inconsistency / non-identical programs: Your two programs are not identical: the first one produces the original word once, while the second version produces the ascending list and then the same list reversed; hence, the original word appears twice. This could be fixed by tweaking the first list to remove the last element, like v[:-1]. You can read more about list slices here; they're generally a useful tool.


        • Efficiency: Unless having the list for a later part of the program is important, it can be made more efficient by using iterators. Iterators can output a pattern's elements one-by-one without storing the entire pattern in memory. For a small program like this, it doesn't make any difference, but being aware of the technique is useful as the situations scale up. This SO answer explains the rationale of using iterators in a bit more detail. Using iterators, one could write:



          import itertools as it
          intext = input("Enter a sentence: ")
          for s in it.chain((intext[:i] for i in range(1, len(intext))),
          (intext[:i] for i in range(len(intext), 0, -1))):
          print(s)


          Here, itertools (an extremely useful standard library, probably one of the most commonly used standard libraries), is used to chain two two iterators together so they are iterated one after another using the nicely named chain function. The mid-line indent is used to limit the line length, and complies with PEP 8's guidelines about indenting split lines.



        • Other critiques: Concatenating two lists together is generally something to avoid if you can, because it's somewhat inefficient. Lambda functions should probably be used sparingly unless they offer a utility over functions in your particular situation. Unpacking argument lists is clever, but in this situation, it's probably more clear and efficient to just iterate through a for-loop as shown above.





        If you want to learn more Python, I highly recommend starting to read through portions of the Python docs as they become relevant. They're generally very well-written and easy to understand. Reading Stack Overflow questions to find solutions to specific programming problems also helps, but the docs help fill in the gaps.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 42 mins ago

























        answered 48 mins ago









        Graham

        566113




        566113






















            Samardeep Khurana is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Samardeep Khurana is a new contributor. Be nice, and check out our Code of Conduct.













            Samardeep Khurana is a new contributor. Be nice, and check out our Code of Conduct.












            Samardeep Khurana is a new contributor. Be nice, and check out our Code of Conduct.
















            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%2f209851%2fdisplaying-a-sentence-letter-by-letter-including-spaces%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'