Use these easy-to-digest Python recipes and take your coding efficiency to the next level.

1. Extract a Subset of a Dictionary

You can extract a subset of a dictionary using the dictionary comprehension method.

Output:

2. Search and Replace Text

You can search and replace a simple text pattern in a string using the str.replace() method.

Output:

For more complicated patterns, you can use the sub() method from the re library. Regular Expressions in Python make the task a lot easier for complicated patterns.

Output:

The above code replaces the white-space character with an underscore character.

3. Filter Sequence Elements

You can filter elements from a sequence according to certain conditions using list comprehension.

Output:

4. Align Text Strings

You can align text strings using the ljust(), rjust(), and center() methods. These methods left-justify, right-justify, and center a string in a field of a given width.

Output:

These methods also accept an optional fill character.

Output:

Note: You can also use the format() function to align strings.

5. Convert Strings Into Datetimes

You can use the strptime() method from the datetime class to convert a string representation of the​ date/time into a date object.

Output:

Note: If the string argument is not consistent with the format parameter, the strptime() method will not work.

6. Unpack a Sequence Into Separate Variables

You can unpack any sequence into variables using the assignment operation. This method works as long as the number of variables and the structure of the sequence match with each other.

Unpacking Tuples

Output:

Unpacking Lists

Output:

Unpacking Strings

Output:

If the number of variables and the structure of the sequence mismatch, you’ll get an error:

Output:

7. Writing Functions That Accept Any Number of Positional Arguments

You need to use a * argument to accept any number of positional arguments.

Output:

8. Return Multiple Values from a Function

You can return multiple values from a function using a tuple, list, or dictionary.

Output:

In the above example, the function returns a tuple. You can unpack the tuple and use the returned values.

Output:

In this example, the function returns a list.

9. Iterate in Reverse

You can iterate over a sequence in reverse order using the reversed() function, range() function, or using the slicing technique.

Iterating in Reverse Using the reversed() Function

Output:

Iterating in Reverse Using the range() Function

Output:

Iterating in Reverse Using the Slicing Technique

Output:

10. Reading and Writing JSON to a File

You can work with JSON data using the built-in json package in Python.

Writing JSON to a File

You can write JSON to a file using the json.dump() method.

This will create a new file named lang.json.

Reading JSON From a File

You can read JSON from a file using the json.load() function. This function loads the JSON data from a JSON file into a dictionary.

Output:

11. Writing to a File That Doesn’t Already Exist

If you want to write to a file only if it doesn’t already exist, you need to open the file in x mode (exclusive creation mode).

If the file lorem.txt already exists, this code will cause Python to throw a FileExistsError.

If you want to have a look at the complete source code used in this article, here’s the GitHub repository.

Make Your Code Robust With Built-In Python Functions

Use functions to break a program into modular chunks and perform specific tasks. Python provides many built-in functions like range(), slice(), sorted(), abs(), and so on that can make your tasks a lot easier. Make use of these built-in functions to write a more readable and functional code.