Counter

from collections import Counter

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

cnt = Counter(num_lst)
print(dict(cnt))
# {1: 3, 2: 4, 3: 4, 4: 2, 5: 1}

# first 2 most occurrence
print(dict(cnt.most_common(2)))
# {2: 4, 3: 4}

str_lst = ['blue', 'red', 'green', 'blue', 'red', 'red', 'green']
print(dict(Counter(str_lst)))
# {'blue': 2, 'red': 3, 'green': 2}

Minimising a for loop

lst = [1, 2, 3]
doubled = []

for num in lst:
   doubled.append(num*2)
print(doubled)
# [2, 4, 6]

# Minimised
doubled = [num*2 for num in lst]
print(doubled)
# [2, 4, 6]

Check All Chars Uppercase

import string

def is_upper(word):
   return all(c in string.ascii_uppercase for c in list(word))
   
print(is_upper('HUMANBEING'))
# True
print(is_upper('humanbeing'))
# False

for and if in One Line

d = [1, 2, 1, 3]
a = [each for each in d if each == 1]
print(a)
# [1,1]

Loop With Index

Example:

lst = ['a', 'b', 'c', 'd']

for index, value in enumerate(lst):
   print(f"{index}, {value}")
# 0, a
# 1, b
# 2, c
# 3, d

for index, value in enumerate(lst, start=10):
   print(f"{index}, {value}")
# 10, a
# 11, b
# 12, c
# 13, d

Reverse a String or List

Example:

a = "humanbeing"
print("Reversed :",a[::-1])
# Reversed : gniebnamuh

b = [1, 2, 3, 4, 5]
print("Reversed :",b[::-1])
# Reversed : [5, 4, 3, 2, 1]

Join String and List Together

Example:

str1 = "do"
str2 = "more"
lst = ["Write", "less", "code"]
str3 = ' '.join(lst) + ', ' + str1 + ' ' + str2
print(str3)
# Write less code, do more

Convert List to Comma Separated String