Often you may have to flatten a list of lists or merge multiple lists into a single list in python. Obviously there are multiple ways to go about it. Here are three ways to flatten or merge lists in to a list. Merge with “+” Operator” If you have three separate lists like [1, 2, […]
Python Tips
How to Fold a Long Single Line Text to Multiple Lines (in Python and Linux)?
You might want to convert a really long string in a single line into multiple lines with specified width. For example, if you have string like 12345678912345678912345678912345678912345678910 and want to split the long text to multi-line text, where each line is of length w, like w=9 123456789 123456789 123456789 123456789 123456789 10 In the above […]
How to Run a Shell Command from Python and Get The Output?
In Python, often you may want to execute linux command and get the output of the command as string variable. There are multiple ways to execute shell command and get output using Python. A naive way to do that is to execeute the linux command, save the output in file and parse the file. Get […]
How To Paste Code in Python Interpreter? Hint: Use ipython
If you have formatted code block with proper indentation and want to test it by copying and pasting on a python interpreter, you will be frustrated at Python for complaining at you. iPython offers great solutions coding with Python and for copying and pasting the code on the terminal. One useful command in iPython to […]
3 Ways to Write Text to a File in Python
If you are interested in writing text to a file in Python, there is probably many ways to do it. Here is three ways to write text to a output file in Python. The first step in writing to a file is create the file object by using the built-in Python command “open”. To create and […]