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.

20 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

  11. ???? ????? ???????? Says:

    ???? ??? , ???? ????? ?????? ??????? ????? ? ????? ?????? ??????? ?????????? ?????? ??????? ????? , ???? ????? ???????? , ??? ????? ???????? , ???? ?????? ???????? , ???? ????? ?????? , ???? ????? ?? ????????  , ???? ????? ?? ??? , ???? ????? ?? ???  , ???? ????? ?????? ?????  , ????? ???????? ????????  , ????? ?? ???? ???? ??? , ????? ??? ????? ?? , ????? ??? ????? , ????? ??? ??????? , ????? ??? ???????? ????? , <a

  12. Jack Kalis Says:

    Well somehow I got to read lots of articles on your blog It's amazing how interesting it is for me to visit you very often. So we are providing law assignment help UK have all information about writing services visit our website.

  13. hellonwood40 Says:

    iPhone users need to download insaver for Instagram, and this application is the best and easy to use. You need to save the URL of the video you want to download and open profile stalker to for Instagram. Then you have to export that video to your camera roll.

  14. navicoads 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
    office automation services
    hearing aid Pakistan
    hearing aids in lahore
    custom suits
    ad agency
    chemical industries in lahore
    Architecture Designs in lahore
    mbbs in china
    hearing clinic in lahore

  15. anju elzza Says:

    rubber insulation
    <a href="">adhesives in uae
    <a href="">rubber sheet suppliers in dubai
    <a href="">adhesive company uae
    <a href="">Bopp Tape
    <a href="">masking tape
    <a href="">foam tapes

    <a href="">safe driver
    <a href="">safe driver dubai
    <a href="">residential moving
    <a href="">hire a driver in dubai
    <a href="">chauffeur service

    <a href="">hotels in dubai city centre mall
    <a href="">hotel near to airport
    <a href="">4 star hotel deira
    <a href="">luxury business hotel dubai
    <a href="">Hotel Near to Airport

    fire & smoke damper
    combination fire & smoke damper
    fire/ smoke damper
    hvac duct suppliers
    stainless steel duct
    vav box

  16. armoredstrong Says:

    Thank you for sharing this blog !!!
    Find here the best adhesives and tapes in UAE
    adhesives in uae
    duct tape suppliers
    tape suppliers
    thermal insulation material uae
    rubber sheet suppliers in dubai
    adhesive manufacturers in uae

  17. Awesome Drive Says:

    Thank you for sharing this blog !!!
    Find the best damper manufacturers
    dampers suppliers
    diffuser manufacturers
    diffusers suppliers
    fire dampers suppliers
    grille manufacturers in uae

  18. Awesome Drive Says:

    Masking tape is generally used to protect interior, furniture, flooring while painting or repairing the indoor applications. For better performance, surfaces should be as clean and dry as possible with any loose material and dust removed as far as possible. Masking Tape is ideal for masking off areas which are not to be painted. The main benefit of utilizing the tape is its weak adhesive that allows easy removal without leaving behind any sticky residue. Our tapes come with different available adhesive strengths. Economical masking tape offering both economy and versatility for a variety of applications. Use for general purpose packaging, building, holding, Hardwood flooring repair, splicing, masking and labeling.

  19. Airmaster Tape Says:

    Thank you for sharing this blog !!!

    Fire Rated Ducting
    XLPE Insulation
    Thermal Insulation Materials
    Fire door
    hvac ducting

  20. Brillian_Drive Says:

    Nice Blog!
    Brilliant Safe Driver in Dubai is a company that has been providing Brilliant safe Driver Dubai would like to extend a warm welcome to you. In comparison to other businesses, Here you can hire safe drivers in Dubai at the most affordable rates.

Leave a Reply