Top 10 Python tips

Format Strings with f-Strings (Python 3.6+)

This is new since Python 3.6 and in my opinion is the best way to format a string. We just have to write an f before our string, and then inside the string we can use curly braces and access variables. This is much simpler and more concise compared to the old formatting rules, and it's also faster. Moreover, we can write expressions in the braces that are evaluated at runtime. So here for example we want to print the squared number of our variable i, and we can simply write this operation in our f-String.

name = "Alex"
my_string = f"Hello {name}"
print(my_string) # Hello Alex

i = 10
print(f"{i} squared is {i*i}") # 10 squared is 100

Concatenate Strings with .join()

Let's say we have a list with different strings, and we want to combine all elements to one string, separated by a space between each word. The bad way is to do it like this:

list_of_strings = ["Hello", "my", "friend"]

# BAD:
my_string = ""
for i in list_of_strings:
    my_string += i + " " 

We defined an empty string, then iterated over the list, and then appended the word and a space to the string. As you should know, a string is an immutable element, so here we have to create new strings each time. This code can be very slow for large lists, so you should immediately forget this approach! Much better, much faster, and also much more concise is to the .join() method:

list_of_strings = ["Hello", "my", "friend"]
my_string = " ".join(list_of_strings)

This combines all the elements into one string and uses the string in the beginning as a separator. So here we use a string with only a space. If we were for example to use a comma here, then the final string has a comma between each word. This syntax is the recommended way to combine a list of strings into one string.

Advanced print options

# with sep parameter

str1 = 'pythonists'
str2 = ''

print(str1, str2, sep='@')

# with end paramerter
print('Ram', end=', ')
print('Shyam', end=', ')
print('Sita', end='.')


[email protected]
Ram, Shyam, Sita.

Printing given string number of times

We can print a given string 'n' number of times in the output simply by using 'String Name * n' syntax into the print statement. It will print the given string n number of times in continuation in output. Example: Output:

Duplicate Removing from List

This tip and trick will help you to remove duplicates from a list in a fast and easy way. check out the code example below.

my_list = [10, 10, 22, 23, 27, 22, 89, 56, 25, 89]#removing duplicatesmy_list = list(set(my_list))print(my_list) # [10, 22, 23, 56, 89, 27, 25]

Passing List as Function Arguments

  • operator could be used to passing list data type as function arguments
data = ["La", "Asada", 1121]
def person(firstname, lastname, user_id):
    print(firstname, lastname, user_id)

"La" "Asada" 1121

Using Python Collections

Python collections are container data types, namely lists, sets, tuples, dictionary. The collections module provides high-performance datatypes that can enhance your code, making things much cleaner and easier. There are a lot of functions provided by the collections module. For this demonstration, I will be using Counter() function. The Counter() function takes an iterable, such as a list or tuple, and returns a Counter Dictionary. The dictionary’s keys will be the unique elements present in the iterable, and the values for each key will be the count of the elements present in the iterable. To create a counter object, pass an iterable (list) to Counter() function as shown in the code below. For a more complete reference, check out my python collections tutorial.

Consider writing your own generator.

The previous tip hints at a general pattern for optimization—namely, that it’s better to use generators where possible. These allow you to return an item at a time rather than all the items at once. As mentioned, the xrange() function is a generator in Python 2, as is the range() function in Python 3. If you’re working with lists, consider writing your own generator to take advantage of this lazy loading and memory efficiency. Generators are particularly useful when reading a large number of large files. It’s possible to process single chunks without worrying about the size of the files. Here’s an example you might use when web scraping and crawling recursively.

import requests
import re

def get_pages(link):
  pages_to_visit = []
  pattern = re.compile('https?')
  while pages_to_visit:
    current_page = pages_to_visit.pop(0)
    page = requests.get(current_page)
    for url in re.findall('<a href="([^"]+)">', str(page.content)):
      if url[0] == '/':
        url = current_page + url[1:]
      if pattern.match(url):
    yield current_page
webpage = get_pages('')
for result in webpage:

This example simply returns a page at a time and performs an action of some sort. In this case, you’re printing the link. Without a generator, you’d need to fetch and process at the same time or gather all the links before you started processing. This code is cleaner, faster, and easier to test.

Obtain the Cartesian Product

The built-in module itertools provides many potentially useful classes. One of them is a product used to obtain the Cartesian product:

Merge dictionaries with the double asterisk syntax ** (Python 3.5+)

This syntax is new since Python 3.5. If we have two dictionaries and want to merge them, we can use curly braces and double asterisks for both dictionaries. So here dictionary 1 has a name and an age, and dictionary 2 also has the name and then the city. After merging with this concise syntax our final dictionary has all 3 keys in it.

d1 = {'name': 'Alex', 'age': 25}
d2 = {'name': 'Alex', 'city': 'New York'}
merged_dict = {**d1, **d2}
print(merged_dict) # {'name': 'Alex', 'age': 25, 'city': 'New York'}