Python tricks

Posted on Jan 28, 2024

The blog post will just be a long list of short summaries of python tricks, that I find good and like to remember(and find fast). Lets go!

Table of Contents

  1. Example
  2. Example2
  3. Third Example
  4. Fourth Example


List comprehensions

List comprehensions are a neat one liner python trick. Not only is it handy it also sometimes faster.

a = [1, 2, 3, 4, 5]
b = [val**2 for val in a]

Lazy initialize list

To initialize a list with x values that have the same number:


we can do the same for lists of x values:

a = [1,2,3]
b = a *10
b[1, 2, 3, 1, 2, 3, 1, 2, 3, ...]


Concatenate strings

a = "hello "
b = "world"
print(a + b)

Selecting chars in string

Strings can be index as a list

a = "hello"

Get length of string

To get the length of a string use len

a = "hello"

Replace in string

"Hello world".replace("world", "John")

Count string or substring

word = "Hello world"
print(word.count(" "))

To repeat string

word  = "Hello world" * 3


a = "hello world"
b = a.split(" ")

Remove white spaces


Python general

Analyze memory

In order to check the memory:

import sys

Swapping values

a = 10
b = 20
a, b = b, a 

Invert dict

The easiest way to invert a dict is to use a dict comprehension:

a = {‘a’: 1, ‘b’: 2,‘c’: 3,‘d’: 4,‘e’: 5,‘f’: 6, ‘g’: 7}
b = {v: k for k, v in dict1.items()}

Combine two python list

In order to combine to lists extends is the way to go, if you use append it will result in that you get a list with a list within.

a = [1, 2, 3]
b = [2, 4, 6]


Transpose matrix:

Cool trick using zip for transposing a matrix:

mat = [[8, 9, 10], [11, 12, 13]]
mut = zip(*mat)
trans = list(mut)


Python offers build in sorting through some of the data structures but also through sorted which is a function available.

To use it to sort a list:

a = [1,2,5,3,2,1,8]
b = sorted(a)

If you have a multi layered data structure such a dict you can handle it like this:

a = [{"company": "Data&IT", "employees": 6}, 
        {"company": "BK", "employees": 800}, 
        {"company": "ICA", "employees": 9000}
b = sorted(a, key=lambda x: x["employees"])

Default it is ascending order, to get id descending set reverse=True

a = [1,2,5,3,2,1,8]
b = sorted(a, reverse=True)

Context Managment - With


Ignore exceptions - contextlib.suppress


As the name sounds all checks if all values in a iterable data structure is true.

a = [1, 2, 3, 4, 5]
b = [val > 4 for val in a]

any instead checks if any of the values are true.

a = [1, 2, 3, 4, 5]
b = [val > 4 for val in a]

Lambda functions

The keyword lambda allows for creating small anonymous functions and has the following syntax:

lambda arguments : expression


x = lambda a : a + 10


Python offers generators as a way to access the value at runtime instead of calculating the whole list first. This allows for saving on memory and is especially useful working with large objects where downstream processes will not need all at once. Using a comprehension expression it looks like this:

a = [1, 2, 3, 4, 5]
b = (val**2 for val in a)

the important difference between the list comprehension and a generator comprehension is the square brackets vs the optional brackets. It can also be create using the yield keyword and a normal function.

def square_list(n: list) -> list:
    for val in n: 
        yield n 

a = [1, 2, 3, 4, 5]

import itertools

To unpack evenly structure lists of list python offers itertools which is part of the standard library

import itertools
a = [[1, 2], [3, 4], [5, 6]]
b = list(itertools.chain.from_iterable(a))

It will unpack one level.

from collections import

The collections package is part of the standard lib and offers multiple useful tools.


defaultdict offers the possibility to define default return values for a dict. Normally the return would be KeyError! with a defaultdict a value to be set when trying to access a value that is not set. The default value can be one of: List, Set or Int. However you can explicitly always set a key to any kind like a normal dict.

from collections import defaultdict

_map = defaultdict(list)
_map['d'] = ""



Counter allows for counting any hashable object(What is a hashable object? Lets not discuss it to much but if you stay with native types you are good. If not check out hash). Say we want to count the distinct values in a array and the the three most common ones:

from collections import Counter
a = [1, 1, 1, 2, 3, 4, 11, 13, 1, 1, 1, 2]
counter = Counter(a)

most_common = counter.most_common(2)
print(most_common) # [(9, 6), (10, 3)]
print(most_common[0]) # (9, 6)
print(most_common[0][0]) # 9

It can also be used to check anagrams:

from collections import Counter

def is_anagram(str1, str2):
    return Counter(str1) == Counter(str2)
print(is_anagram(taste, state))
print(is_anagram(beach, peach))


The Zen of Python