Daniel Lemire's blog

, 2 min read

No loops in Python one-liners?

4 thoughts on “No loops in Python one-liners?”

  1. Dand says:
  2. Flávio Coelho says:

    You can do loops in the form of list comprehensions.

    import sys,os,re,fileinput;a=[i[2] for i in os.walk(‘.’) if i[2]] [0];[sys.stdout.write(re.sub(‘at’,’op’,j)) for j in fileinput(a,inplace=1)]

    Careful with this one though, it will edit files on your path without prompt!


  3. parry_all says:

    You can do this:

    for i in [1, 2, 3, 4, 5]: print(i)

  4. dana says:

    or this way..

    python -c $'import os\nfor i in os.listdir("."): print i'