avatarAbhay Parashar

Summary

The provided content is a comprehensive guide offering 28 Python code snippets designed to solve everyday coding problems, enhance productivity, and streamline data manipulation tasks.

Abstract

The web content presents a collection of Python code snippets aimed at tackling common programming challenges encountered by Python developers. The author, with nearly five years of experience using Python, shares their insights on how to efficiently handle tasks such as combining dataframes, flattening lists, filtering data by date range, converting data formats, sorting lists of dictionaries, managing missing values, and many more. The snippets cover a wide range of applications, from handling categorical data and British date formats to creating subsets and decoding base64 files. The guide emphasizes the importance of reusable code, demonstrates how to measure execution time, and provides tips for memory usage management. It also touches on advanced concepts like metaclasses and the unique identification of objects in Python. The content is structured to be both educational and immediately practical, with each snippet accompanied by explanations and examples to aid understanding and implementation.

Opinions

  • The author believes in the practical application of Python for everyday problems, emphasizing the language's versatility and efficiency.
  • There is a clear preference for using Python's built-in functions and libraries, such as pandas, to perform data manipulation tasks.
  • The author values the readability and maintainability of code, suggesting the use of partial functions and metaclasses for improved code organization and behavior.
  • The guide reflects an appreciation for Python's ability to handle various data types and structures, with examples showing how to work with lists, dictionaries, JSON, and CSV formats.
  • The author encourages the use of Python's error handling features, such as the try-except-else-finally blocks, to create robust and user-friendly applications.
  • The content suggests that Python's flexibility in dealing with data, such as reshaping DataFrames and extracting information, is a key strength of the language for data analysis tasks.
  • The author promotes the idea of leveraging Python's extensive standard library and third-party modules to solve complex problems more elegantly and concisely.

Python Shortcuts for Everyday Wins!! — P1

28 Insanely Useful Python Code Snippets For Everyday Problems

Boost Your Productivity with These Time-Saving Python Solutions

AI Generate Image using LeonardAI

I’ve been using Python for nearly five years, and it’s been my go-to language for solving everyday problems. Whether I’m trying to boost efficiency, simplify workflows, or tackle complex coding challenges, Python has never let me down. Over the past year, I’ve focused on refining my coding practices, and uncovering techniques and tricks that have significantly boosted my productivity and efficiency. In this blog, I’ll share some of the best Python code snippets I use to tackle my everyday problems.

As Great Albert Einstein Says — “Never memorize something that you can look up.”

Why remember every coding solution when you can have these handy Python snippets at your fingertips?

1. Combining multiple dataframes into one

We often receive data across multiple files, and to analyze it effectively, it is essential to combine these into a single data frame.

import pandas as pd

# Sample DataFrames
df1 = pd.DataFrame({'ID': [1, 2, 3], 'Name': ['Alice', 'Bob', 'Charlie']})
df2 = pd.DataFrame({'ID': [2, 3, 4], 'Age': [25, 30, 35]})
df3 = pd.DataFrame({'ID': [1, 2], 'City': ['New York', 'Los Angeles']})

# Method 1: Merge (similar to SQL JOIN)
merged_df = pd.merge(df1, df2, on='ID', how='inner')

# Method 2: Join (combine based on index)
joined_df = df1.set_index('ID').join(df2.set_index('ID'))

# Method 3: Concatenate (adding rows)
concatenated_df = pd.concat([df1, df3], ignore_index=True)
print("Merged DataFrame:\n", merged_df)
print("\nJoined DataFrame:\n", joined_df)
print("\nConcatenated DataFrame:\n", concatenated_df)
Output

2. Flattening a List

Flattening a list involves converting a list of lists into a single, unified list. List comprehension makes this process both straightforward and efficient, simplifying data manipulation and access.

nested_list = [[1, 2, 3], [4, 5], [6, 7], [8,9],[10,11]]
flattened_list = [item for sublist in nested_list for item in sublist]
print(flattened_list)

"""Output"""
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

3. Filtering Data Based on a Date Range in Pandas

Working with time series data often requires filtering records within a specific date range. For example, to retrieve data between 2nd February 2021 and 2nd February 2022, you can use the `.loc` accessor with boolean indexing. This method provides a clean and efficient way to filter your data by date.

import pandas as pd

# Sample DataFrame with a Date column
data = {
    'Date': ['2021-01-15', '2021-02-05', '2021-05-20', '2021-12-25', '2022-02-01', '2022-03-10'],
    'Value': [10, 20, 30, 40, 50, 60]
}

df = pd.DataFrame(data)

# Convert 'Date' column to datetime format
df['Date'] = pd.to_datetime(df['Date'])

# Define the date range
start_date = '2021-02-02'
end_date = '2022-02-02'

# Filter the DataFrame for the specified date range
filtered_df = df.loc[(df['Date'] >= start_date) & (df['Date'] <= end_date)]

print(filtered_df)

4. Convert Monthly Data to Daily or Weekly Data

When working with data reported monthly, you might need to convert it to daily or weekly intervals for better analysis. This snippet helps you transform monthly data into daily or weekly formats, enabling more detailed insights and better data management.

import pandas as pd

# Sample DataFrame with monthly data
dates = pd.date_range('2021-01-01', periods=4, freq='M')
data = pd.DataFrame({'Value': [100, 200, 300, 400]}, index=dates)

# Method 1: Convert Monthly to Daily
daily_data = data.resample('D').ffill()

# Method 2: Convert Monthly to Weekly
weekly_data = data.resample('W').ffill()

print("Daily Data:\n", daily_data)
print("\nWeekly Data:\n", weekly_data)
Sample Output — Screenshot By Author

5. Text slicing with ease

Text slicing is a common task when manipulating strings in Python. With slicing, you can easily extract specific parts of a string without using complex string operations.

# Sample list
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# 1. Reversing a list
reversed_data = data[::-1]
print("Reversed:", reversed_data)

# 2. Slicing with a step
step_slice = data[::2]  # Every second element
print("Every second element:", step_slice)

# 3. Slicing with a negative step (reverse slice)
reverse_step_slice = data[8:2:-2]  # Slices from index 8 to 2 with step -2
print("Reverse step slice:", reverse_step_slice)

# 4. Slicing with start and end indices
range_slice = data[3:7]  # Elements from index 3 to 6
print("Elements from index 3 to 6:", range_slice)

# 5. Slicing with a combination of start, end, and step
combined_slice = data[1:9:3]  # Elements from index 1 to 8 with step 3
print("Elements from index 1 to 8 with step 3:", combined_slice)

6. Sorting a list of dictionaries

When you have a list of dictionaries, sorting them by a specific key can help organize your data more effectively. This snippet is handy for arranging data based on criteria such as dates, names, or numerical values.

dict1 = [
    {"Name":"Karl",
     "Age":25},
     {"Name":"Lary",
     "Age":39},
     {"Name":"Nina",
     "Age":35}
]

## Using sort()
dict1.sort(key=lambda item: item.get("Age"))

# List sorting using itemgetter
from operator import itemgetter
f = itemgetter('Name')
dict1.sort(key=f)

# Iterable sorted function
dict1 = sorted(dict1, key=lambda item: item.get("Age"))

'''Output
[{'Age': 25, 'Name': 'Karl'},
 {'Age': 35, 'Name': 'Nina'},
 {'Age': 39, 'Name': 'Lary'}]
'''

7. Get the Count of Missing Values In Each Column

Missing data is a major problem for data science projects and handling it is one of the most important tasks one needs to do. Before tackling missing values you need to get a sneak of missing values in your data. This snippet gives you a quick count of missing values in each column, making it easier to identify incomplete data and decide how to handle it.

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/Abhayparashar31/datasets/refs/heads/master/titanic.csv')

missing_values_count = df.isnull().sum()

print("Count of Missing Values in Each Column:")
print(missing_values_count)
Script Output — Image By Author

8. Convert categorical data into integer

Converting categorical data to integers is a common step in preparing it for machine learning models. This snippet simplifies the process, making the data easier to use in algorithms that need numerical input.

import pandas as pd

# Sample DataFrame with categorical data
df = pd.DataFrame({
    'Category': ['A', 'B', 'A', 'C', 'B', 'C']
})

# Method 1: Using pd.factorize()
df['Category_Factorize'] = pd.factorize(df['Category'])[0]

# Method 2: Using pd.Categorical
df['Category_Categorical'] = pd.Categorical(df['Category']).codes

# Method 3: Using LabelEncoder from sklearn
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['Category_LabelEncoder'] = le.fit_transform(df['Category'])

print(df)
Script Output — Image By Author

9. Precise Float Formatting

Precise float formatting is crucial for displaying numerical data accurately and clearly.

# 1. Using f-strings (formatted string literals)
value = 123.456789
formatted_value = f"{value:.2f}"  # Keeps 2 decimal places
print(formatted_value)  # Output: 123.46

# 2. Using format() method
formatted_value = "{:.2f}".format(value)  # Format with 2 decimal places
print(formatted_value)  # Output: 123.46

# 3. Using round() function
rounded_value = round(value, 2)
print(rounded_value)  # Output: 123.46

# 4. Using % string formatting
formatted_value = "%.2f" % value  # Format to 2 decimal places
print(formatted_value)  # Output: 123.46

# 5. Using Decimal for precise decimal handling
from decimal import Decimal

value = Decimal('123.456789')
formatted_value = round(value, 2)
print(formatted_value)  # Output: 123.46
British dates: where ‘3/4’ can leave you wondering if it’s March 4th or April Fool’s Day!! — Tenor GIF

10. Handling Columns With British Date Format

Many countries use the British date format, where dates are written as day-month-year. You can handle this format by enabling the dayfirst parameter.

import pandas as pd
df = pd.DataFrame({'date_time': ['6/12/2000 12:32', '13/5/2001 11:45', '11/12/2005 1:57'],
                   'value': [2, 3, 4]})
pd.to_datetime(df['date_time'],dayfirst=True)

11. Convert JSON to CSV

Handling data in JSON format is common, but many tools and applications prefer CSV files. This snippet addresses the everyday challenge of converting JSON data into CSV, making it easier to work with spreadsheets or perform data analysis tasks.

import pandas as pd
from pandas import json_normalize
import json

# Load JSON data
with open('/content/data.json') as f:   ##Colab Sample JSOn
    data = json.load(f)

# Normalize the JSON data
df = json_normalize(data)

# Save to CSV
df.to_csv('output_file.csv', index=False)
Sample Output Screenshot — By Author

12. Finding all subsets of a set in one line

When working with sets, a common task is finding all possible subsets, whether you’re generating combinations for testing, mathematical calculations, or solving algorithmic problems.

This snippet tackles the everyday challenge of finding all subsets of a set, condensed into a single line of code. It’s a handy tool when you need it.

from itertools import combinations
data = [55,65,32,44,67]
print(list(combinations(data, 3)))


"""OUTPUT"""
[(55, 65, 32), (55, 65, 44), (55, 65, 67), (55, 32, 44), (55, 32, 67), 
(55, 44, 67), (65, 32, 44), (65, 32, 67), (65, 44, 67), (32, 44, 67)]

13. Decode a base64 encoded file

Encountering files encoded in Base64 is common when dealing with email attachments, images, or data transmission. The challenge comes when you need to decode that file back into its original format.

import base64, sys
base64.decode(open(sys.argv[1], "rb"), open(sys.argv[2], "wb"))

14. Taking Space Separated Multiple Inputs

This code snippet lets you take multiple space-separated inputs at one time. This snippet will come in handy whenever you are solving a programming competition question.

## Taking Two Integers as input
a,b = map(int,input().split())
print("a:",a)
print("b:",b)

## Taking a List as input
arr = list(map(int,input().split()))
print("Input List:",arr)

Quiz Time ⏲️

— — — — — — -

a, b = 256, 256 print(a is b)

What will be the output for this? True? Yup!!!

Now, what about

a, b = 257, 257 print(a is b)

What will be the output now ?? True or False ??

15. Sorting list based on Another list

Sometimes when working with two related lists, you might need to sort one based on the order of the other. A real-world example would be if you have a list of student names and a corresponding list of their scores, but you want to sort the names based on their scores.

# Lists to be sorted
names = ['Alice', 'Bob', 'Charlie', 'David']
scores = [85, 92, 78, 90]

# Desired order for sorting
desired_order = [3, 1, 4, 2]  # This defines the desired order

# Sort names based on the desired order
sorted_names = [x for _, x in sorted(zip(desired_order, names))]

print(sorted_names)

"""OUTPUT"""
['Bob', 'David', 'Alice', 'Charlie']
Its’ time for some calculations!!! — Tenor GIF

16. Calculate the execution time of a cell

Knowing the execution time of a code block or cell is crucial for optimizing performance. This snippet helps you measure how long a piece of code takes to run, aiding in identifying and improving slow sections.

# METHOD 1
import datetime
start = datetime.datetime.now()
"""
CODE
"""
print(datetime.datetime.now()-start)

# METHOD 2
import time
start_time = time.time()
main()
print(f"Total Time To Execute The Code is {(time.time() - start_time)}" )

# METHOD 3
import timeit
code = '''
## Code snippet whose execution time is to be measured
[2,6,3,6,7,1,5,72,1].sort()
'''
print(timeit.timeit(stmy = code,number = 1000))

17. Assembling a DateTime column

In datasets with separate columns for year, month, and day, combining them into a single DateTime column is crucial for accurate time-based analysis.

import pandas as pd
df = pd.DataFrame({'year': [2021,2021,2021,2022, 2022],
                   'month': [3,3,4,4,4],
                   'day': [6,7,6,7,8],
                  })
pd.to_datetime(df[['month','day','year']])

18. Most Frequent Element Inside a List

This code snippet finds the most frequent elements in a list, helping you quickly identify which items occur most often.

def most_frequent(given_list):
  return max(set(given_list),key = given_list.count)

print(most_frequent([1,2,3,3,3,3,4,4,5,5]))

"""OUTPUT"""
3
Skipping ropes keeps you fit, just like skipping unnecessary top rows keeps your dataset from being a bloated mess! — Tenor GIF

19. Skipping the top n Rows of the dataset

Sometimes, when downloading data from online sources, the top rows may contain metadata or irrelevant information not useful for analysis or modeling. Using the skiprows parameter in read_csv(), you can easily skip the top `n` rows and focus on the actual dataset for further processing.

import pandas as pd
pd.read_csv('data.csv',skiprows=3)  ## skips top 3 rows of the dataset

20. Extracting Dates From a List of Dictionaries

Extracting dates from a list of dictionaries is essential when dealing with structured data that includes time-related information. This snippet helps you efficiently pull out date values from multiple dictionary entries, streamlining data processing and analysis.

## Let's create a sample dataframe
df = pd.DataFrame({'col1': ["[{'date':'31/04/2000','sale':5000}, {'time':'11:50','info':'checked'}]",
                            "[{'date':'1/05/2000','sale':'50'},  {'time':'10:30','info':'checked'}]",
                            "[{'date':'2/05/2000','sale':'50'},  {'time':'9:50','info':'checked'}]"],
                   'value': [2, 3, 4]})
## Main Code
dates = []
time = []
for row in df['col1']:
    for data in eval(row):
        try:
            dates.append(data['date'])
        except:
            time.append(data['time'])

df['date'] = dates
df['time'] = time

21. Handling Exceptions with Else Clauses

In Python, you can use an else clause in a try-except block. The else block runs only if no exceptions are raised, giving you a clean separation between error handling and normal execution.

try:
    num = int(input("Enter a number: "))
except ValueError:
    print("That's not a valid number!")
else:
    print(f"You entered: {num}")
finally:
    print("This will always run.")

22. Checking memory usage

Monitoring memory usage is a crucial task, especially when dealing with large datasets or optimizing code performance. Checking how much memory your Python objects consume can help you identify bottlenecks, prevent memory leaks, and improve overall efficiency.

import sys
a = 10
print(sys.getsizeof(a))
Key errors in dictionaries are just life’s way of reminding you to double-check your keys — because even data needs a lock!!! — Tenor GIF

23. Handling Key Errors in Dictionaries

When working with dictionaries, accessing a non-existent key will result in a KeyError. To avoid this, you can use the .get() method, which allows you to provide a default value in case the key is not found.

data = {"name": "Alice", "age": 25}

# Handling KeyError with a default value
age = data.get("age", "Key not found")
print(age)

# Trying to access a key that doesn't exist
location = data.get("location", "Unknown")
print(location)  # Output: Unknown

24. Leveraging Partial Functions for Code Reusability

Partial functions, available through Python’s functools module, let you pre-define some of a function’s arguments, creating a new function with fewer inputs. This is particularly useful for generating customized versions of functions, improving code flexibility and reuse without changing the original function.

from functools import partial

def calculate_shipping_cost(weight: float, distance: int, rate_per_kg: float) -> float:
    total_cost = weight * distance * rate_per_kg
    return total_cost

# Create partial functions for specific shipping rates
domestic_shipping = partial(calculate_shipping_cost, rate_per_kg=0.05)
international_shipping = partial(calculate_shipping_cost, rate_per_kg=0.15)

# Calculate shipping costs with different rates
domestic_cost = domestic_shipping(weight=10, distance=300)
international_cost = international_shipping(weight=5, distance=1200)

# Print the results
print(f"Domestic shipping cost: ${domestic_cost:.2f}")
print(f"International shipping cost: ${international_cost:.2f}")

25. Counting the occurrence of an element

In everyday data analysis tasks, you often need to determine how many times a particular item appears in a list. This can be useful for tasks like analyzing survey results or processing user activity logs. Using the walrus operator can streamline this process by allowing you to both assign and use the count of each element efficiently.

from collections import Counter

words = ['red', 'blue', 'red', 'green', 'blue', 'blue']
word_counts = Counter(words)
print(word_counts)  

"""OUTPUT"""
Counter({'blue': 3, 'red': 2, 'green': 1})

26. Reshaping DataFrame

Reshaping a data frame is often necessary to adjust its structure for better analysis or visualization. This snippet assists in transforming the layout of your data, making it easier to work with different formats and meet specific analytical needs.

import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
    'ID': [1, 2, 3],
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Score': [85, 90, 95]
})

# Method 1: Transpose
transposed_df = df.T

# Method 2: Stack (pivot DataFrame)
stacked_df = df.set_index('ID').stack()

# Method 3: GroupBy and Aggregate
grouped_df = df.groupby('Name').agg({'Score': 'mean'})

print("Transposed DataFrame:\n", transposed_df)
print("\nStacked DataFrame:\n", stacked_df)
print("\nGrouped DataFrame:\n", grouped_df)

27. Metaclasses for class creation

In Python, metaclasses give you control over class creation and behavior. Essentially, a metaclass is like a class for classes, defining how classes behave, just as classes define the behavior of objects. This feature is particularly useful when you need to customize or automate class construction.

class LowercaseAttributeMeta(type):
    def __new__(cls, name, bases, dct):
        # Enforce lowercase naming convention for attributes
        for attr_name in dct:
            if not attr_name.islower() and not attr_name.startswith('_'):
                raise TypeError(f"Attribute '{attr_name}' is not in lowercase.")
        return super().__new__(cls, name, bases, dct)

class MyLowercaseClass(metaclass=LowercaseAttributeMeta):
    _private_attr = 10  # Valid
    lowercase_attr = 20  # Valid

# This will raise an error because the attribute name is not in lowercase
try:
    class InvalidLowercaseClass(metaclass=LowercaseAttributeMeta):
        UpperCaseAttr = 10  # This will cause a TypeError
except TypeError as e:
    print(e)

28. Prints the Unique ID of a Variable

In Python, each object is assigned a unique identifier that can be accessed using the id() function. This ID is useful when you want to track objects, especially when working with mutable data types like lists or dictionaries, to ensure you know whether two variables point to the same object in memory.

# Define a variable
a = [1, 2, 3]

# Print unique ID of the variable 'a'
print(f"The unique ID of variable 'a': {id(a)}")

# Assign 'a' to a new variable 'b'
b = a

# Print unique ID of variable 'b' to verify if both point to the same object
print(f"The unique ID of variable 'b': {id(b)}")

# Define a new list and print its unique ID to show the difference
c = [1, 2, 3]
print(f"The unique ID of variable 'c': {id(c)}")

# Check if 'a' and 'c' are the same object
print(f"'a' and 'c' are the same object: {a is c}")



"""
The unique ID of variable 'a': 140701793403712
The unique ID of variable 'b': 140701793403712
The unique ID of variable 'c': 140701793404032
'a' and 'c' are the same object: False
"""

Thanks For Reading Till Here, If You Like My Content and Want To Support Me The Best Way is —

  1. Leave a Clap👋and your thoughts 💬 below.️
  2. Follow Me On Medium.
  3. Connect With Me On LinkedIn.
  4. Attach yourself to My Email List to never miss reading another article of mine
  5. Do Follow The Pythoneers Publication for more similar stories.
Tenor GIF
Python
Programming
Data Science
Software Development
Productivity
Recommended from ReadMedium