1. How to save lines to list in only one line

def read_config(config):
    with open(config, 'r') as f:
        return list(map(lambda item: item.strip('\n'), f.readlines()))

2. How to get value from nested dictionary

def safe_get(dct, keys):
    for key in keys.split("."):
        try:
            dct = dct[key]
        except:
            return None
    return dct

3. How to rewrite the file

import json
with open (filename, 'r+') as json_file:
  	...
    json_file.seek(0,0)
    json.dump(data, json_file)

4. How to eliminate duplicate values in list

sth = list(set(sth))

5. How to create or remove dirs safely

import os, shutil

os.makedirs("temp", exist_ok=True)

if os.path.exists("temp") and os.path.isdir("temp"): shutil.rmtree("temp")

6. How to remove prefix

Python 3.9 or 3.9+

# 0. Remove prefix from the string
s0="abcPYTHONabc"
s0.removeprefix("abc")
#Output:PYTHONabc
s0.lstrip("abc")
#Output:PYTHONabc

# 1.The parameters of removeprefix() are considered as a substring. But parameters of lstrip() are considered as a set of characters
s1 = "abcbcacPYTHONabc"
s1.removeprefix("abc")
#Output:bcacPYTHONabc
s1.lstrip("abc")
#Output:PYTHONabc

# 2. removeprefix won't remove multiple copies of a prefix. But lstrip() will remove multiple copies of a prefix.
s2="abcabcPYTHONabc"
s2.lstrip("abc")
#Output:PYTHONabc
s2.removeprefix("abc")
#Output:abcPYTHONabc

# 3. If the parameter is not mentioned in lstrip(), it will remove leading whitespaces. But if the parameter is not mentioned in removeprefix(), it will raise TypeError.
s3="   PYTHON"
s3.lstrip()
#Output:PYTHON
s3.removeprefix()
#Output:TypeError: str.removeprefix() takes exactly one argument (0 given)

# 4. If the parameter mentioned is not found at the beginning of the string means, both methods will return a copy of the string.
s4="PYTHONabc"
s4.lstrip("abc")
#Output:PYTHONabc
s4.removeprefix("abc")
#Output:PYTHONabc

before Python 3.9

# 1. strip lstrip rstrip
str="abcbcxyyyyabcbc"
str.strip("abc")
#Output:xyyyy
str.lstrip("abc")
#Output:xyyyyabcbc
str.rstrip("abc")
#Output:abcbcxyyyy

# 2. re.sub()
import re
pattern_suffix=r"abc$"
pattern_prefix=r"^abc"
s="abcPYTHONacbcabc"
s_suffix=re.sub(pattern_suffix,"",s) #Output:abcPYTHONacbc
s_prefix=re.sub(pattern_prefix,"",s) #Output:PYTHONacbcabc

# 3. str.startswith()
s3="abcabcPythonacbcabc"
if (s3.startswith("abc")):
    print (s3.replace("abc","",1))
#Output:abcPythonacbcabc

Reference

remove prefix