Top 10 Python tips

Merge List

List support operator * and + for merging the elements. The example below will show both operator could have the same results.

names = ['Anna', 'John', 1, 10.5, {'store': 'dictionary'}]
other_list = [2, "Asada", "Max"]

print([*names, *other_list])
['Anna', 'John', 1, 10.5, {'store': 'dictionary'}, 2, 'Asada', 'Max']

print(names + other_list)
['Anna', 'John', 1, 10.5, {'store': 'dictionary'}, 2, 'Asada', 'Max']

Sort Dictionaries

You can use a similar approach to sort key-value tuples of dictionaries obtained with .items() method: They are sorted according to the keys. If you want to them to be sorted according to their values, you should specify the arguments that correspond to key and eventually reverse:

Iterate With enumerate()

Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object. Let’s solve the classic coding interview question named popularly as the Fizz Buzz problem.

Write a program that prints the numbers in a list, for multiples of ‘3’ print “fizz” instead of the number, for the multiples of ‘5’ print “buzz” and for multiples of both 3 and 5 it prints “fizzbuzz”.

Creating a single string from elements of a list

We can simply create a single string from all the elements of a given list by using "." with the join() function inside the print statement with the list variable. So, by this, we can easily get the single string data format from the multiple data elements given in list format. Example: Output:

Remember the built-In functions.

Python comes with a lot of batteries included. You can write high-quality, efficient code, but it’s hard to beat the underlying libraries. These have been optimized and are tested rigorously (like your code, no doubt). Read the list of the built-ins, and check if you’re duplicating any of this functionality in your code.

Using List Comprehensions

List comprehension is one of the key Python features, and you may already be familiar with this concept. Even if you are, here's a quick reminder of how list comprehensions help us create lists much more efficiently.:

# Inefficient way to create new list based on some old list
squares = []
for x in range(5):

[0, 1, 4, 9, 16]

# Efficient way to create new list based on some old list
squares = [x**2 for x in range(5)]

[0, 1, 4, 9, 16]

Raw Formatted Strings

PEP 498 and Python 3.6 introduced so-called formatted strings or f-strings. You can embed expressions inside such strings. It’s possible and straightforward to treat a string as both raw and formatted. You need to include both prefixes: fr.

Python use of Enums

In Python, we can simply use the Enums to check the number of occurrences of a variable inside the given function where it first occurred. We just have to use the word with the function name inside the print statement with the "." operator to print the number of the first occurrence of that variable inside the function. Example: Output:

Using ZIP When Working with Lists

Suppose you were given a task to combine several lists with the same length and print out the result? Again, here is a more generic way to get the desired result by utilizing zip() as shown in the code below: