Skip to content

General

Sort a list of dicts by key

1
2
3
4
5
6
7
my_unsorted_list = [
    {"name": "Julien", "age": 12},
    {"name": "Xavier", "age": 20},
    {"name": "Alex", "age": 43},
]
sorted(my_unsorted_list, key=lambda d: d["name"])
# [{'name': 'Alex', 'age': 43}, {'name': 'Julien', 'age': 12}, {'name': 'Xavier', 'age': 20}]

Random item from a list

1
2
3
4
5
import random

my_list = [1, 2, 3, 4]
random.choice(my_list)
# 2

.tar.gz archives

Create a .tar.gz archive. Suppose you have this source folder structure:

1
2
3
4
5
.
└── source-dir
    β”œβ”€β”€ file1
    β”œβ”€β”€ file2
    └── file3

Then:

1
2
3
4
import tarfile

with tarfile.open(f"source-dir.tgz", "w:gz") as tar:
    tar.add("source-dir")

Will create a source-dir.tgz archive containing the exact same structure (keeping the source-dir).

To only add files to the archives (without keeping source-dir):

import os
import tarfile

with tarfile.open(f"source-dir.tgz", "w:gz") as tar:
    for filename in os.listdir("source-dir"):
        tar.add(
            os.path.join("source-dir", filename),
            arcname=filename,
            recursive=False,
        )

Will create a source-dir.tgz archive with this structure:

1
2
3
4
.
β”œβ”€β”€ file1
β”œβ”€β”€ file2
└── file3

Other compression algorithms are supported (bz2, lzma etc). Official doc