How-to articles, tricks, and solutions about PYTHON

How to convert list to string

You can use the join() method to convert a list of strings to a single string.

Converting from a string to boolean in Python

In Python, you can use the bool() function to convert a string to a boolean.

How to sort a list of objects based on an attribute of the objects?

You can use the sort method of a list and pass in a key argument, which is a function that takes an object and returns the value on which you want to sort the list.

What's the difference between lists and tuples?

Lists and tuples are both used to store multiple items in a single variable, but they are different in a few key ways.

Usage of __slots__?

__slots__ is a way to specify a fixed set of attributes for a class, which can help to save memory in certain situations.

Get unique values from a list in python

In Python, you can use the set() function to get the unique values from a list.

How can I represent an 'Enum' in Python?

In Python, you can represent an Enum (enumeration) by using the enum module or by creating a class that inherits from enum.Enum.

How to parse XML and get instances of a particular node attribute?

You can use the xml library in Python to parse XML and get instances of a particular node attribute.

What is a cross-platform way to get the home directory?

The os.path module in Python provides a cross-platform way to get the home directory.

What are "named tuples" in Python?

"Named tuples" in Python are a subclass of the built-in tuple type, but with the added ability to access elements by name rather than just by index.

How do you test that a Python function throws an exception?

You can use the pytest.raises function to test that a Python function throws an exception.

Should I put #! (shebang) in Python scripts, and what form should it take?

You should include a shebang (#!) in Python scripts if you want the script to be directly executable from the command line.

Is there a simple way to delete a list element by value?

Yes, you can use the remove() method to delete a list element by its value.

How do I install a Python package with a .whl file?

To install a Python package with a .whl file, you can use the pip command.

Remove all whitespace in a string

You can remove all whitespace in a string in Python by using the replace() method and replacing all whitespace characters with an empty string.

Get the data received in a Flask request

In a Flask application, you can access the data received in a request using the request object.

"Large data" workflows using pandas

Here is an example of a workflow for handling large data using the pandas library:

Get difference between two lists with Unique Entries

You can use the set data type to find the difference between two lists with unique entries.

Difference between del, remove, and pop on lists

del: The del keyword is used to remove an item from a list by its index.

Fastest way to check if a value exists in a list

The fastest way to check if a value exists in a list is to use the in operator.

How do I get a list of locally installed Python modules?

You can use the pip module to get a list of locally installed Python modules.

How do I trim whitespace?

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

How do I iterate through two lists in parallel?

You can use the zip() function to iterate through two lists in parallel.