TG Telegram Group Link
Channel: Python Codes
Back to Bottom
Inverts a dictionary with non-unique hashable values.

πŸ‘‰Create a collections.defaultdict with list as the default value for each key.

πŸ‘‰Use dictionary.items() in combination with a loop to map the values of the dictionary to keys using dict.append().

πŸ‘‰Use dict() to convert the collections.defaultdict to a regular dictionary.

CODE:

from collections import defaultdict

def collect_dictionary(obj):
inv_obj = defaultdict(list)
for key, value in obj.items():
inv_obj[value].append(key)
return dict(inv_obj)

Example:

ages = {
'Peter': 10,
'Isabel': 10,
'Anna': 9,
}
collect_dictionary(ages)

Output: { 10: ['Peter', 'Isabel'], 9: ['Anna'] }

Share and Support
@Python_Codes
Walrus operator:

The Walrus or := operator is one of the latest additions to python 3.8.
It is an assignment operator that lets you assign value to a variable within an expression like conditional statements, loops, etc.


Example

If we want to check and print the length of a list:

Mylist = [1,2,3]
if(l := len(mylist) > 2)
print(l)


Output

3

Share and Support
@Python_Codes
What Is FastAPI?

FastAPI is a modern, high-performance web framework for building APIs with Python based on standard type hints.


It has the following key features:

πŸ‘‰Fast to run: It offers very high performance, on par with NodeJS and Go, thanks to Starlette and pydantic.

πŸ‘‰Fast to code: It allows for significant increases in development speed.

πŸ‘‰Reduced number of bugs: It reduces the possibility for human-induced errors.

πŸ‘‰Intuitive: It offers great editor support, with completion everywhere and less time debugging.

πŸ‘‰Straightforward: It’s designed to be uncomplicated to use and learn, so you can spend less time reading documentation.

πŸ‘‰Short: It minimizes code duplication.

πŸ‘‰Robust: It provides production-ready code with automatic interactive documentation.

πŸ‘‰Standards-based: It’s based on the open standards for APIs, OpenAPI and JSON Schema.

You can use this instead of Django and Flask

Share and Support
@Python_Codes
Difference between list and tuple in python

πŸ”ΈList is mutable ( you can modify the original list) and it's values are written in sqare brackets [ ]

πŸ”ΈTuple is immutable ( you can't modify it) and it's values are written in parentheses ( ) delimited by comma( , )

πŸ”ΈTo convert list to tuple - we use tuple() function
list1 = [1,2,3]
print(tuple(list1)) Output : (1,2,3)

πŸ”Έ For single element list
list1 = [1]
print(tuple(list1)) Output : (1, )

β–ͺ️a tuple is a tuple because of comma not because of parentheses


@Python_Codes
Guess the Number Game in Python

@Python_Codes
Indian Flag Using Python

@Python_Codes
To shuffle pandas DataFrame df (in a reproducible way):

df = df.sample(frac=1, random_state=123).reset_index(drop=True)

Alternatively, you can use sklearn.utils.shuffle().

#pandas

@Python_Codes
3 Ways to merge Dictionaries in python

@Python_Codes
Do you know abou .strip()

From the above example you can see that it removed all the characters mentioned in .strip('comw.') and returns the remaining string


@Python_Codes
The simplest way to flatten a list in python.


@Python_Codes
Useful Pandas🐼 method you should definitely know

βœ… head()
βœ… info()
βœ… fillna()
βœ… melt()
βœ… pivot()
βœ… query()
βœ… merge()
βœ… assign()
βœ… groupby()
βœ… describe()
βœ… sample()
βœ… replace()
βœ… rename()


@Python_Codes
Program to create a Countdown Timer

@Python_Codes
πŸ’‘ Python Tip: Do you know Ellipsis(...) can be used as a placeholder in Python, just like a 𝘱𝘒𝘴𝘴 statement?

@Python_Codes
Summarize any website content in just a few lines of Python code.

@Python_Codes
πŸ’‘ Python Life Hack: Do you know you can copy the files from a computer to a mobile phone πŸ“± without any cable using Python 🐍 ?

@Python_Codes
Python Class Anatomy

Almost everything a Python class definition can contain summed up in one image :)

@Python_Codes
Python Tip:

You can use dict on a class instance to get all instance attributes as a dictionary

@Python_Codes
Python Tip:

You can use the calendar module in one line from the command line with python -m calendar

@Python_Codes
Multiple Adjacent String Literals in python

@Python_Codes
Use functools.cache to add a simple cache to your Python 🐍 functions.

@Python_Codes
HTML Embed Code:
2025/07/07 03:56:04
Back to Top