Am I interpreting this list comprehension correctly in python?












-1















I am still fairly new to python and still have some fumbles when reading list comprehensions. I tried translating two list comprehensions I saw in a tutorial into its elongated form. Did I translate correctly?



list comprehension 1



mytokens = [ word.lemma_.lower().strip() if word.lemma_ != "-PRON-" else word.lower_ for word in mytokens ]


translation 1



for word in mytokens:
if word.lemma_ != "-PRON-":
word.lemma_.lower().strip()
else:
word.lower_


list comprehension 2



mytokens = [ word for word in mytokens if word not in stopwords and word not in punctuations ]


translation 2



for word in mytokens:
if word not in stopwords and not in punctuations:
yield word


for translation 2, I dont think "yield word" would be correct since its not a definition. I am guessing list comprehension 2 does the if statement and places the word back into the list mytokens so maybe it should be a .append?










share|improve this question




















  • 1





    Do your translations do the same thing? That should provide your answer.

    – jonrsharpe
    Nov 14 '18 at 20:48











  • I am not sure if they do the same thing hence the question!

    – PaperRockBazooka
    Nov 14 '18 at 20:51











  • So did you test them? What you've posted here isn't valid syntax on its own, and neither actually creates a list.

    – jonrsharpe
    Nov 14 '18 at 20:51













  • Oh i thought this would be simple for an experienced coder-- like reading a equation from a subject matter someone is familiar in. I didnt realize you need the variables in context to be able to figure out list comps. Sorry! p.s. if this isnt a list comprehension what is it?

    – PaperRockBazooka
    Nov 14 '18 at 20:56


















-1















I am still fairly new to python and still have some fumbles when reading list comprehensions. I tried translating two list comprehensions I saw in a tutorial into its elongated form. Did I translate correctly?



list comprehension 1



mytokens = [ word.lemma_.lower().strip() if word.lemma_ != "-PRON-" else word.lower_ for word in mytokens ]


translation 1



for word in mytokens:
if word.lemma_ != "-PRON-":
word.lemma_.lower().strip()
else:
word.lower_


list comprehension 2



mytokens = [ word for word in mytokens if word not in stopwords and word not in punctuations ]


translation 2



for word in mytokens:
if word not in stopwords and not in punctuations:
yield word


for translation 2, I dont think "yield word" would be correct since its not a definition. I am guessing list comprehension 2 does the if statement and places the word back into the list mytokens so maybe it should be a .append?










share|improve this question




















  • 1





    Do your translations do the same thing? That should provide your answer.

    – jonrsharpe
    Nov 14 '18 at 20:48











  • I am not sure if they do the same thing hence the question!

    – PaperRockBazooka
    Nov 14 '18 at 20:51











  • So did you test them? What you've posted here isn't valid syntax on its own, and neither actually creates a list.

    – jonrsharpe
    Nov 14 '18 at 20:51













  • Oh i thought this would be simple for an experienced coder-- like reading a equation from a subject matter someone is familiar in. I didnt realize you need the variables in context to be able to figure out list comps. Sorry! p.s. if this isnt a list comprehension what is it?

    – PaperRockBazooka
    Nov 14 '18 at 20:56
















-1












-1








-1








I am still fairly new to python and still have some fumbles when reading list comprehensions. I tried translating two list comprehensions I saw in a tutorial into its elongated form. Did I translate correctly?



list comprehension 1



mytokens = [ word.lemma_.lower().strip() if word.lemma_ != "-PRON-" else word.lower_ for word in mytokens ]


translation 1



for word in mytokens:
if word.lemma_ != "-PRON-":
word.lemma_.lower().strip()
else:
word.lower_


list comprehension 2



mytokens = [ word for word in mytokens if word not in stopwords and word not in punctuations ]


translation 2



for word in mytokens:
if word not in stopwords and not in punctuations:
yield word


for translation 2, I dont think "yield word" would be correct since its not a definition. I am guessing list comprehension 2 does the if statement and places the word back into the list mytokens so maybe it should be a .append?










share|improve this question
















I am still fairly new to python and still have some fumbles when reading list comprehensions. I tried translating two list comprehensions I saw in a tutorial into its elongated form. Did I translate correctly?



list comprehension 1



mytokens = [ word.lemma_.lower().strip() if word.lemma_ != "-PRON-" else word.lower_ for word in mytokens ]


translation 1



for word in mytokens:
if word.lemma_ != "-PRON-":
word.lemma_.lower().strip()
else:
word.lower_


list comprehension 2



mytokens = [ word for word in mytokens if word not in stopwords and word not in punctuations ]


translation 2



for word in mytokens:
if word not in stopwords and not in punctuations:
yield word


for translation 2, I dont think "yield word" would be correct since its not a definition. I am guessing list comprehension 2 does the if statement and places the word back into the list mytokens so maybe it should be a .append?







python list-comprehension






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 20:48







PaperRockBazooka

















asked Nov 14 '18 at 20:46









PaperRockBazookaPaperRockBazooka

476




476








  • 1





    Do your translations do the same thing? That should provide your answer.

    – jonrsharpe
    Nov 14 '18 at 20:48











  • I am not sure if they do the same thing hence the question!

    – PaperRockBazooka
    Nov 14 '18 at 20:51











  • So did you test them? What you've posted here isn't valid syntax on its own, and neither actually creates a list.

    – jonrsharpe
    Nov 14 '18 at 20:51













  • Oh i thought this would be simple for an experienced coder-- like reading a equation from a subject matter someone is familiar in. I didnt realize you need the variables in context to be able to figure out list comps. Sorry! p.s. if this isnt a list comprehension what is it?

    – PaperRockBazooka
    Nov 14 '18 at 20:56
















  • 1





    Do your translations do the same thing? That should provide your answer.

    – jonrsharpe
    Nov 14 '18 at 20:48











  • I am not sure if they do the same thing hence the question!

    – PaperRockBazooka
    Nov 14 '18 at 20:51











  • So did you test them? What you've posted here isn't valid syntax on its own, and neither actually creates a list.

    – jonrsharpe
    Nov 14 '18 at 20:51













  • Oh i thought this would be simple for an experienced coder-- like reading a equation from a subject matter someone is familiar in. I didnt realize you need the variables in context to be able to figure out list comps. Sorry! p.s. if this isnt a list comprehension what is it?

    – PaperRockBazooka
    Nov 14 '18 at 20:56










1




1





Do your translations do the same thing? That should provide your answer.

– jonrsharpe
Nov 14 '18 at 20:48





Do your translations do the same thing? That should provide your answer.

– jonrsharpe
Nov 14 '18 at 20:48













I am not sure if they do the same thing hence the question!

– PaperRockBazooka
Nov 14 '18 at 20:51





I am not sure if they do the same thing hence the question!

– PaperRockBazooka
Nov 14 '18 at 20:51













So did you test them? What you've posted here isn't valid syntax on its own, and neither actually creates a list.

– jonrsharpe
Nov 14 '18 at 20:51







So did you test them? What you've posted here isn't valid syntax on its own, and neither actually creates a list.

– jonrsharpe
Nov 14 '18 at 20:51















Oh i thought this would be simple for an experienced coder-- like reading a equation from a subject matter someone is familiar in. I didnt realize you need the variables in context to be able to figure out list comps. Sorry! p.s. if this isnt a list comprehension what is it?

– PaperRockBazooka
Nov 14 '18 at 20:56







Oh i thought this would be simple for an experienced coder-- like reading a equation from a subject matter someone is familiar in. I didnt realize you need the variables in context to be able to figure out list comps. Sorry! p.s. if this isnt a list comprehension what is it?

– PaperRockBazooka
Nov 14 '18 at 20:56














2 Answers
2






active

oldest

votes


















1














I think it is right. You are looping correctly. However, you aren't adding the words to a list? Do you mean to be doing this?



So for the first one you could use



my_list = 
for word in mytokens:
if word.lemma_ != "-PRON-":
my_list.append(word.lemma_.lower().strip())
else:
my_list.append(word.lower_)


By adding them to a list like this it means you can directly compare the output of your translation and the output of the list comprehension. mytokens should be exactly the same as my_list if done correctly.



Also there is a small mistake in the second translation. It should be:



for word in mytokens:
if word not in stopwords and word not in punctuations:
yield word


You could also modify this second translation to add all your words to a list.






share|improve this answer

































    1














    Let's simplify this:



    coll = ["Gerry", "Mary", "Sue"]

    comprehended = [ word.lower() for word in coll]

    def comprehender(coll):
    coll_out =
    for word in coll:
    coll_out.append(word.lower())
    return coll_out


    If you run this, you can be assured that the two are equivalent by using assert or just returning comprehended == comprehender(coll)



    This is a valid sanity check you can do on any list comprehension, you just vary this pattern to match the logic of your comprehension.






    share|improve this answer
























    • oh interesting! Thank you i will give this a shot

      – PaperRockBazooka
      Nov 14 '18 at 21:45











    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%2f53308470%2fam-i-interpreting-this-list-comprehension-correctly-in-python%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









    1














    I think it is right. You are looping correctly. However, you aren't adding the words to a list? Do you mean to be doing this?



    So for the first one you could use



    my_list = 
    for word in mytokens:
    if word.lemma_ != "-PRON-":
    my_list.append(word.lemma_.lower().strip())
    else:
    my_list.append(word.lower_)


    By adding them to a list like this it means you can directly compare the output of your translation and the output of the list comprehension. mytokens should be exactly the same as my_list if done correctly.



    Also there is a small mistake in the second translation. It should be:



    for word in mytokens:
    if word not in stopwords and word not in punctuations:
    yield word


    You could also modify this second translation to add all your words to a list.






    share|improve this answer






























      1














      I think it is right. You are looping correctly. However, you aren't adding the words to a list? Do you mean to be doing this?



      So for the first one you could use



      my_list = 
      for word in mytokens:
      if word.lemma_ != "-PRON-":
      my_list.append(word.lemma_.lower().strip())
      else:
      my_list.append(word.lower_)


      By adding them to a list like this it means you can directly compare the output of your translation and the output of the list comprehension. mytokens should be exactly the same as my_list if done correctly.



      Also there is a small mistake in the second translation. It should be:



      for word in mytokens:
      if word not in stopwords and word not in punctuations:
      yield word


      You could also modify this second translation to add all your words to a list.






      share|improve this answer




























        1












        1








        1







        I think it is right. You are looping correctly. However, you aren't adding the words to a list? Do you mean to be doing this?



        So for the first one you could use



        my_list = 
        for word in mytokens:
        if word.lemma_ != "-PRON-":
        my_list.append(word.lemma_.lower().strip())
        else:
        my_list.append(word.lower_)


        By adding them to a list like this it means you can directly compare the output of your translation and the output of the list comprehension. mytokens should be exactly the same as my_list if done correctly.



        Also there is a small mistake in the second translation. It should be:



        for word in mytokens:
        if word not in stopwords and word not in punctuations:
        yield word


        You could also modify this second translation to add all your words to a list.






        share|improve this answer















        I think it is right. You are looping correctly. However, you aren't adding the words to a list? Do you mean to be doing this?



        So for the first one you could use



        my_list = 
        for word in mytokens:
        if word.lemma_ != "-PRON-":
        my_list.append(word.lemma_.lower().strip())
        else:
        my_list.append(word.lower_)


        By adding them to a list like this it means you can directly compare the output of your translation and the output of the list comprehension. mytokens should be exactly the same as my_list if done correctly.



        Also there is a small mistake in the second translation. It should be:



        for word in mytokens:
        if word not in stopwords and word not in punctuations:
        yield word


        You could also modify this second translation to add all your words to a list.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 21:50

























        answered Nov 14 '18 at 20:55









        James FultonJames Fulton

        1825




        1825

























            1














            Let's simplify this:



            coll = ["Gerry", "Mary", "Sue"]

            comprehended = [ word.lower() for word in coll]

            def comprehender(coll):
            coll_out =
            for word in coll:
            coll_out.append(word.lower())
            return coll_out


            If you run this, you can be assured that the two are equivalent by using assert or just returning comprehended == comprehender(coll)



            This is a valid sanity check you can do on any list comprehension, you just vary this pattern to match the logic of your comprehension.






            share|improve this answer
























            • oh interesting! Thank you i will give this a shot

              – PaperRockBazooka
              Nov 14 '18 at 21:45
















            1














            Let's simplify this:



            coll = ["Gerry", "Mary", "Sue"]

            comprehended = [ word.lower() for word in coll]

            def comprehender(coll):
            coll_out =
            for word in coll:
            coll_out.append(word.lower())
            return coll_out


            If you run this, you can be assured that the two are equivalent by using assert or just returning comprehended == comprehender(coll)



            This is a valid sanity check you can do on any list comprehension, you just vary this pattern to match the logic of your comprehension.






            share|improve this answer
























            • oh interesting! Thank you i will give this a shot

              – PaperRockBazooka
              Nov 14 '18 at 21:45














            1












            1








            1







            Let's simplify this:



            coll = ["Gerry", "Mary", "Sue"]

            comprehended = [ word.lower() for word in coll]

            def comprehender(coll):
            coll_out =
            for word in coll:
            coll_out.append(word.lower())
            return coll_out


            If you run this, you can be assured that the two are equivalent by using assert or just returning comprehended == comprehender(coll)



            This is a valid sanity check you can do on any list comprehension, you just vary this pattern to match the logic of your comprehension.






            share|improve this answer













            Let's simplify this:



            coll = ["Gerry", "Mary", "Sue"]

            comprehended = [ word.lower() for word in coll]

            def comprehender(coll):
            coll_out =
            for word in coll:
            coll_out.append(word.lower())
            return coll_out


            If you run this, you can be assured that the two are equivalent by using assert or just returning comprehended == comprehender(coll)



            This is a valid sanity check you can do on any list comprehension, you just vary this pattern to match the logic of your comprehension.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 20:57









            Charles LandauCharles Landau

            2,2281216




            2,2281216













            • oh interesting! Thank you i will give this a shot

              – PaperRockBazooka
              Nov 14 '18 at 21:45



















            • oh interesting! Thank you i will give this a shot

              – PaperRockBazooka
              Nov 14 '18 at 21:45

















            oh interesting! Thank you i will give this a shot

            – PaperRockBazooka
            Nov 14 '18 at 21:45





            oh interesting! Thank you i will give this a shot

            – PaperRockBazooka
            Nov 14 '18 at 21:45


















            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%2f53308470%2fam-i-interpreting-this-list-comprehension-correctly-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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings