How to make for loop faster in python?

What are the quickest Python alternatives to Perl one-liners like "perl -n -e ..."?

  • It seems no obvious brief command-line alternatives exist except the more verbose "python -c ..." (which requires writing imports, a loop, etc.). Are there other alternatives or tools to make this quicker? Cf. http://stackoverflow.com/questions/367115/is-there-a-python-equivalent-to-perl-pi-e

  • Answer:

    Python does not lend itself well to dense one-liners. I would argue this is a plus. :-) If you need functionality like this, the generally-accepted solution is to create assorted shell scripts, or a custom module with assorted shell scripts inside of it, or create a basic DSL that does what you want (which the SO answer that links to the ActiveState code suggests doing).

Vaibhav Mallya at Quora Visit the source

Was this solution helpful to you?

Other answers

Print json in nice format using python command line. Very useful. echo '{"name": "fname", "family": "lame"}' | python -mjson.tool

Aniket Ashok Zamwar

To beautify a json file in python one-liner cat file.json | python -m json.tool Start a simple http server anywhere python -m SimpleHTTPServer Print every line from an input file but remove the first two fields. python -c "import sys;[sys.stdout.write(' '.join(line.split(' ')[2:])) for line in sys.stdin]" < input.txt

Bipul Jain

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.