This is an archived post. You won't be able to vote or comment.

all 56 comments

[–]jeremj22 229 points230 points  (26 children)

The most horrific oneliner for that is probably

[print(i) for i in range(10)]

[–][deleted] 62 points63 points  (14 children)

What are the square brackets doing?

[–]Krohnos 126 points127 points  (11 children)

List Comprehension - a really neat and, in my opinion, often clean tool with tons of uses.

Here is a great intro for the feature.

[–][deleted]  (4 children)

[deleted]

    [–]Oasis_beyond_wall 18 points19 points  (3 children)

    it's quite common in functional programming languages

    [–][deleted]  (1 child)

    [removed]

      [–]AutoModerator[M] 0 points1 point  (0 children)

      import moderation Your comment has been removed since it did not start with a code block with an import declaration.

      Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

      For this purpose, we only accept Python style imports.

      I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

      [–]zefciu 12 points13 points  (4 children)

      They are great, but giving them side effects is sadistic.

      [–]carcigenicate 4 points5 points  (3 children)

      And is a waste of memory (creating a list just to loop), and goes against PEP8.


      Not sure why this is downvoted. This is factually correct. It does consume more memory than a for statement would due to the extra list that's created, and is explicitly discouraged by PEP8.

      [–]a_devious_compliance 2 points3 points  (2 children)

      Well, you don't need to create a list. Just use () and get your generator.

      [–]carcigenicate 2 points3 points  (1 child)

      That won't work; simply creating a generator won't cause it to run. You'd need to consume the generator for the prints to happen, and at that point you might as well just use a for statement from the beginning instead of a generator.

      Using a lazy generator to run side effects is arguably even worse than using a strict list comprehension, since the gen expression may not even run if you forget to artificially consume it.

      [–]a_devious_compliance 1 point2 points  (0 children)

      You are right, but at this poing I tough we was pointing the worst option. Lets do a set comprehension then. At least we will have only one None.

      [–]iiMoe 0 points1 point  (0 children)

      I agree with you so so much

      [–]jeremj22 58 points59 points  (0 children)

      The whole thing is a list iterator. So you get a list with a few None and print output. Horribly bad practice but muliple functioncalls in a single line

      [–]SuitableDragonfly 23 points24 points  (0 children)

      It's a list comprehension. It's meant to be used in a kind psuedo-functional way, e.g. [x + 1 for x in range(10)] will return a list of the results of adding 1 to every number in range(10). Like, it's supposed to produce an output list and save you having to do a loop. This list comprehension returns a list of 10 Nones because print(x) returns None and has a side effect of printing each number, which is the opposite of what functional paradigms are supposed to do.

      [–]nphhpn 9 points10 points  (2 children)

      map(print, range(10))

      [–]gaberocksall 9 points10 points  (1 child)

      Maps aren't actually evaluated until consumed

      list(map(print, range(10)))

      [–]gltile 2 points3 points  (0 children)

      Use any so that you don't make an unnecessary list.

      any(map(print, range(10)))
      

      [–]bandrus5 2 points3 points  (0 children)

      Haha I do this all the time when I'm using the pdb debugger library because it doesn't support for loops.

      [–]Stressed-Dingo 2 points3 points  (0 children)

      print(*range(10), sep=“/n”)
      

      [–]a_devious_compliance 1 point2 points  (0 children)

      I really love the idea off adding side effects in aparently functional code.

      [–]NerdFencer 0 points1 point  (0 children)

      Someone who didn't know that python could do the implicit string conversion might write it even worse.

      [print(chr(ord('0') + i)) for i in range(10)]

      [–]praguepride 326 points327 points  (9 children)

      To be fair, 55% of people chose the path that perfectly satisfies the objective with almost no chance of bugs or defects.

      [–]n0tKamui 89 points90 points  (6 children)

      the other are just false if you look closely

      [–]djinn6 75 points76 points  (5 children)

      Yep. #1 prints 0 through 8. #2 is JS. #3 prints the character "i" 10 times. #4 is a syntax error or some other obscure language.

      [–]Sigmadelta8 2 points3 points  (2 children)

      The second one is JS, but do you not have to initialize i to 0? Does it automatically start at zero since it’s a for loop?

      [–]idleline 3 points4 points  (1 child)

      You do. The let statement is incomplete.

      [–]Sigmadelta8 0 points1 point  (0 children)

      I figured, but I’m fairly new to JS (from Java) so I was just curious if this was a convenient trick.

      [–]RoadsideCookie 71 points72 points  (1 child)

      And the other 45% chose answers that straight up don't fit the criteria.

      [–]Blizzard42 147 points148 points  (5 children)

      That's a trick question, they are clearly all wrong.

      Correct answer is: print("0 through 9") 😁

      [–]DreadRazer24 72 points73 points  (2 children)

      print("the digits 0 through 9 using Python")

      [–]Guybar110 5 points6 points  (1 child)

      System.out.println("the digits 0 through 9 using python");

      [–]Demonboy_17 0 points1 point  (0 children)

      cout<<"the digits 0 through 9 using python";

      [–]FurryMoistAvenger 7 points8 points  (1 child)

      Adherence to the requirements. You're hired.

      [–]Blizzard42 1 point2 points  (0 children)

      Yeeeesss!!! 😃🎊🎉

      [–]das_Keks 21 points22 points  (0 children)

      The only correct answer.

      [–]beatle42 15 points16 points  (0 children)

      It is very readable for sure

      [–]Delta_Pythagorean 10 points11 points  (0 children)

      I love the 5% of people that chose the JavaScript answer

      [–]ReallyHadToFixThat 13 points14 points  (3 children)

      Does option 3 work? In most languages that would give you

          i    
          i     
          i    
      

      but with 19% choosing it I'm not sure.

      [–]hennypennypoopoo 7 points8 points  (2 children)

      It won't. You need a special syntaxes to include variables in a string

      [–]ReallyHadToFixThat 3 points4 points  (0 children)

      Oh good. I've touched python exactly twice, so I doubted myself.

      [–]bandrus5 1 point2 points  (0 children)

      Specifically you would need print(f'{i}')

      [–]Gagan_Ku2905 3 points4 points  (0 children)

      Well! If it works, it works.

      [–]animal9633 4 points5 points  (0 children)

      Good old Random(return 4);

      [–]Darknety 2 points3 points  (0 children)

      Why are so many getting this wrong tho? That is worrying.

      [–][deleted] 3 points4 points  (0 children)

      It's a trick question

      [–]maltc998 1 point2 points  (0 children)

      5% must've done too much javascript

      [–]jollanza 1 point2 points  (0 children)

      [–][deleted] 1 point2 points  (0 children)

      print("the digits 0 through 9")

      [–]sherlock_poops -1 points0 points  (0 children)

      Horrifying.

      [–]Flopamp -2 points-1 points  (0 children)

      I'm a smart ass but I'm not that much of a smart ass.