List Comprehensions

I find myself using list comprehensions in Python a lot lately. Here’s a nifty example that combines a ternary operation and a list comprehension:
resultArray = [i if (i > 0.01) else 0 for i in inputArray if i >= 0]
English translation: Create an output array that stores all of the non-negative values of the input array. If an input value is less than or equal to 0.01, set it to zero.

Explanation: Reading this starting in the middle, it says “For every entry, i, in input_array, first test whether the entry is greater than or equal to zero. If and only if the entry, i, passes the test, continue with statement in the first part of the bracket; set i to zero if it is smaller than 0.01, otherwize leave it unchanged.”

The equivalent code in C++03 would be considerably more involved. To make it look simple, a programmer would have to do some extra work in advance, or add an extra dependency by using a feature from Boost or another library. In C++11, obtaining the same result is pretty simple because of the range-based for loop feature. If we assume the output array has already been created then the equivalent code in a single, condensed line of C++11 is:
for (auto& i : inputArray) { if (i >= 0) resultArray.push_back((i > 0.01) ? i : 0); }
This isn’t too bad. Of course, it’s still not quite as elegant as the Python syntax.

10 Responses to “List Comprehensions”

  1. superiorpapers Says:

    Education makes individuals believe that they ought to deal with other individuals too. Educations make a man extremely kind to others and help them in their troublesome time. This makes them near to one another.

  2. Merlin John Says:

    I think this is the only site where I found such interesting articles that is both informative and useful. I am checking this blog in daily basis for new things like this and thanks for sharing. I really enjoyed reading this article a lot.
    Tours In Amsterdam

  3. Mollie Simmons Says:

    Thanks a lot for providing this list of comprehensions i need to share it at Dermount University for those students who are about to learn this in near future.

  4. Lily Says:

    I adore C++ but I think that Pyton is better for math assignments. Or use mythesismaster to help you make any tasks.

  5. ErnestCortez Says:

    Online education degree graduates are increasing day by day because all the adults are busy in their life and they complete their further education through online programs. Our top writing services have all information about getting online graduation degree.

  6. instagram online Says:

    Thank you for sharing the list, it's very helpful for me.
    instagram travel

  7. ???? ????? ??????? Says:

    a great post,i like it very much,thanks you.…

  8. 312-50 dumps Says:

    Thanks a lot, dude, for putting up this vital information on the internet for everybody to use. You have got a really useful blog I have been here reading for about an hour.

  9. ChrisGreenwelt Says:

    A thesis or dissertation is a document submitted in support of candidature for an academic degree or professional qualification presenting the author's research and findings.

  10. Herry Jone Says:

    Thanks for sharing this is such a great video that you are providing and you give it away for free. I love seeing blog that understand the value of providing quality information for free. I would like to suggest you that please keep on sharing such type of information with us. I really found it too much informative. It is what I was searching for many days. Disertation Help UK Company

Leave a Reply