How-to articles, tricks, and solutions about PYTHON

How do I print the full NumPy array, without truncation?

To print the full NumPy array without truncation, you can use the numpy.set_printoptions() function and set the threshold parameter to np.inf.

How do I print the key-value pairs of a dictionary in python

You can use the items() method to print the key-value pairs of a dictionary in Python.

How do I print to stderr in Python?

To print to stderr in Python, you can use the stderr attribute of the sys module:

How do I put a variable’s value inside a string (interpolate it into the string)?

In Python, you can use the format() method or f-strings (available in Python 3.6 and above) to interpolate a variable's value into a string.

How do I read CSV data into a record array in NumPy?

You can use the numpy.genfromtxt() function to read CSV data into a NumPy record array.

How do I read from stdin?

You can read from stdin in Python using the input function.

How do I remove a substring from the end of a string?

To remove a substring from the end of a string, you can use the rsplit() method and specify the substring as the delimiter.

How do I remove the first item from a list?

You can use the list method pop(index) to remove the first item in a list.

How do I remove/delete a folder that is not empty?

You can use the shutil module in Python to remove a folder that is not empty.

How do I reverse a list or loop over it backwards?

There are a few ways to reverse a list in Python, depending on what you want to do with the reversed list.

How do I reverse a string in Python?

To reverse a string in Python, you can use the following method:

How do I select rows from a DataFrame based on column values?

You can use the DataFrame.loc method to select rows from a DataFrame based on column values.

How do I set the figure title and axes labels font size?

In matplotlib, you can set the font size of the figure title and axes labels using the pyplot module.

How do I sort a dictionary by key?

In Python, dictionaries are unordered collections of key-value pairs.

How do I sort a dictionary by value?

You can use the sorted() function and pass it the dictionary, along with the key parameter set to a lambda function that returns the value from the dictionary.

How do I sort a list of dictionaries by a value of the dictionary?

To sort a list of dictionaries by a value of the dictionary, you can use the sorted() function and specify the key parameter to be a lambda function that returns the value you want to sort by.

How do I split a list into equally-sized chunks?

Here is a function that takes a list and an integer n and splits the list into n equally sized chunks:

How do I split a string into a list of characters?

In Python, you can use the built-in list() function to convert a string into a list of characters.

How do I split a string into a list of words?

Here is an example of how to split a string into a list of words in Python:

How do I split the definition of a long string over multiple lines?

You can split a string over multiple lines by using triple quotes, either ''' or """ .

How do I terminate a script?

In Python, you can terminate a script by using the exit() function from the sys module.

How do I trim whitespace from a string?

There are a few ways to trim whitespace from a string in Python.

How do I trim whitespace?

In Python, you can use the .strip() method to remove leading and trailing whitespace from a string.