Learn various Python tips and tricks through Python Snippets.

You will learn ๐Ÿ“š

  • Find the frequency of characters in a string.
  • Merge 2 lists into a dictionary.
  • f-string: Print Variable Names.
  • Extract elements from a list.

Python Snippets ๐Ÿ“ธ


Code ๐Ÿ’ป

Find the frequency of characters in a string

from collections import Counter

string = "Hello"
print(Counter(string))

Merge 2 lists into a dictionary

# Merge Two Lists into a Dictionary
keys = [1, 2, 3]
values = ['John', 'Samy', 'Adam']

dict_kv = dict(zip(keys, values))

Print Variable Names using f-string

fruit = 'Apple'
count = 11

print(f'{fruit = } & {count = }')

Extract elements from a list

nums = [1, 2, 3]

print(nums)  # [1, 2, 3]
print(*nums) # 1 2 3

If you have not installed python then download it from here. For more Python related posts, click here.

Categorized in: