Arpana Jain

Arpana Jain

30 Articles Published

Articles by Arpana Jain

Page 3 of 3

Python - Remove item from dictionary when key is unknown

Arpana Jain
Arpana Jain
Updated on 27-Mar-2026 281 Views

Sometimes you need to remove items from a Python dictionary, but you don't know the exact key. This situation commonly arises when processing user input, filtering data, or handling dynamic keys. Python provides several safe methods to handle this scenario without raising errors. Using the del Keyword with Exception Handling The del keyword removes a key-value pair from a dictionary. When the key might not exist, wrap it in a try-except block to handle the KeyError ? # Define a dictionary my_dict = {'apple': 1, 'banana': 2, 'orange': 3} # Key that might not exist ...

Read More

Python - Remove given element from list of lists

Arpana Jain
Arpana Jain
Updated on 27-Mar-2026 2K+ Views

Python lists are versatile data structures that can contain other lists, creating nested or 2D structures. When working with lists of lists, you may need to remove specific elements from the inner lists. Python provides several approaches to accomplish this task efficiently. Understanding Lists of Lists A list of lists is a collection where each element is itself a list. This creates a matrix-like structure that's useful for storing tabular data or multi-dimensional information. # Example of a list of lists matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print("Original matrix:", matrix) ...

Read More

Python - Remove first K elements matching some condition

Arpana Jain
Arpana Jain
Updated on 27-Mar-2026 199 Views

Removing elements from a list that match specific conditions is a common task in Python programming. This article explores how to remove the first K elements from a list that satisfy a given condition using two different approaches. Problem Definition Given a list and a condition, we need to remove only the first K elements that match the condition, leaving other matching elements intact. For example, if we have a list [1, 2, 3, 4, 5, 6, 7, 8] and want to remove the first 2 even numbers, the result should be [1, 3, 5, 6, 7, 8] ...

Read More

Python - Remove False Rows from a Matrix

Arpana Jain
Arpana Jain
Updated on 27-Mar-2026 262 Views

Matrices are essential data structures in Python for mathematical computations and data analysis. Sometimes matrices contain rows with all False or zero values that need to be removed for cleaner data processing. This article demonstrates efficient methods to remove such rows using Python. What are False Rows? A false row in a matrix typically refers to rows containing all zero values or all False boolean values. These rows often represent empty or invalid data that should be filtered out before analysis. Algorithm Here's a simple 5-step approach to remove false rows ? Step 1 ...

Read More

Python - Updating value list in dictionary

Arpana Jain
Arpana Jain
Updated on 27-Mar-2026 539 Views

In Python, dictionaries store data as key-value pairs where values can be lists. Updating these value lists is a common operation when working with dynamic data structures. Syntax dictionary_name[key] = new_value_list You can update a value list by assigning a new list to the specific key using the assignment operator (=). This replaces the existing list associated with that key. Method 1: Direct Assignment Replace the entire value list with a new list ? # Create a dictionary with initial values student_grades = { 'math': [85, 90, ...

Read More

Visualizing O(n) using Python

Arpana Jain
Arpana Jain
Updated on 27-Mar-2026 234 Views

Time complexity is a fundamental concept in computer science that measures how an algorithm's runtime changes as the input size grows. O(n) represents linear time complexity, where execution time increases proportionally with input size. Understanding and visualizing O(n) helps developers write efficient code and analyze algorithm performance. What is O(n) Time Complexity? O(n) indicates that an algorithm's execution time grows linearly with the input size 'n'. If you double the input, the execution time roughly doubles too. The most common example is a simple loop that processes each element once ? def linear_search(arr, target): ...

Read More

Python - Visualize graphs generated in NetworkX using Matplotlib

Arpana Jain
Arpana Jain
Updated on 27-Mar-2026 3K+ Views

NetworkX is a powerful Python library for creating, manipulating, and studying complex networks. When combined with Matplotlib, it provides excellent capabilities for visualizing graphs with customizable layouts, colors, and labels. Basic Graph Visualization The simplest way to visualize a NetworkX graph is using the draw() function with Matplotlib ? import networkx as nx import matplotlib.pyplot as plt # Create a simple graph G = nx.Graph() G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(3, 1) # Draw and display the graph nx.draw(G, with_labels=True, node_color='lightblue', node_size=500) plt.title("Basic Graph Visualization") plt.show() Visualizing Graphs with Node Labels and Edge ...

Read More

Python - Valid Ranges Product

Arpana Jain
Arpana Jain
Updated on 27-Mar-2026 208 Views

The valid ranges product problem involves finding the product of consecutive non-zero elements in a list, treating zeros as delimiters. This is useful in data processing and algorithmic challenges where you need to process segments of data separated by specific values. Problem Definition Given a list containing numbers and zeros, we need to ? Identify continuous sequences of non-zero numbers Calculate the product of elements in each valid sequence Return a list of all products For example, in [4, 9, 0, 0, 3, 4, 5, 0, 0, 4, 0], the valid ranges are [4, ...

Read More

Python - Use of slots

Arpana Jain
Arpana Jain
Updated on 27-Mar-2026 889 Views

Python's __slots__ is a class attribute that restricts which attributes can be assigned to instances, providing memory optimization and faster attribute access. By defining slots, Python avoids creating a __dict__ for each instance, significantly reducing memory usage. Syntax class MyClass: __slots__ = ('attr1', 'attr2', 'attr3') The __slots__ attribute accepts a tuple, list, or any iterable containing the names of allowed attributes. Once defined, instances can only have these specific attributes. Basic Usage of Slots Here's a simple example demonstrating how slots work ? class Person: ...

Read More

Python - URL Shortener using Tinyurl API

Arpana Jain
Arpana Jain
Updated on 27-Mar-2026 2K+ Views

In the web era, concise links are crucial for sharing hyperlinks via social media, text messages, and other communication methods. Long URLs can be challenging to share and might be truncated in messages. Python provides a convenient approach to interact with URL shortening services like TinyURL through their API. What is a URL Shortener? A URL shortener is a service that takes a long URL as input and generates a shorter, more manageable URL. This shortened URL redirects users to the original long URL when clicked. URL shorteners are widely used on social media, email communications, and any ...

Read More
Showing 21–30 of 30 articles
« Prev 1 2 3 Next »
Advertisements