Server Side Programming Articles

Page 21 of 2110

Delete items from dictionary while iterating in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 571 Views

A Python dictionary is a collection which is unordered, changeable and indexed. They have keys and values and each item is referenced using the key. Modifying a dictionary while iterating over it can cause runtime errors, so we need special techniques to safely delete items during iteration. Using del with Collected Keys In this approach we first collect the keys that need to be deleted, then iterate through this separate list to delete the items. This prevents modification of the dictionary during iteration ? Example # Given dictionary days_dict = {1: 'Mon', 2: 'Tue', 3: ...

Read More

Delete elements with frequency atmost K in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 439 Views

While manipulating data from lists we may come across scenarios where we need to selectively remove elements from the list based on their frequency. In this article we will explore how to remove all elements from a list whose frequency is at most K (less than or equal to K). We'll demonstrate with K=2, but you can change this value to any number in the programs. Using count() Method The count() method returns the frequency of each element in the list. We use it with list comprehension and put a condition to only keep elements whose count is ...

Read More

Delete elements in range in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 880 Views

Deleting elements from a Python list by specific indices requires careful handling to avoid index shifting issues. This article explores two effective approaches to delete multiple elements based on their index positions. Using sorted() with del Statement The most straightforward approach is to sort the indices in reverse order and delete elements from highest to lowest index. This prevents index shifting issues ? Example numbers = [11, 6, 8, 3, 2] # The indices to delete indices_to_delete = [1, 3, 0] # printing the original list print("Given list is :", numbers) print("The indices ...

Read More

Custom list split in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 573 Views

Data analytics throws complex scenarios where the data need to be wrangled and moved around. In this context let's see how we can take a big list and split it into many sublists as per the requirement. In this article we will explore the approaches to achieve this. Using List Comprehension and zip() In this approach we use list slicing to get the elements from specific split points. Then we use zip() to create start and end indices for each sublist. Example data_list = ['Mon', 'Tue', 'Wed', 6, 7, 'Thu', 'Fri', 11, 21, 4] ...

Read More

Create list of numbers with given range in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 16K+ Views

Python provides several built-in functions and libraries to generate sequences of numbers within a specified range. This article explores different approaches using range(), random.randrange(), and NumPy's arange() function. Using range() Function The range() function generates a sequence of numbers starting from 0 by default, incrementing by 1, and ending before a specified number. You can customize the start, end, and step values to meet your requirements. Example def generate_numbers(start, end, step): return list(range(start, end, step)) # Generate numbers from -3 to 6 with step 2 start, end, step = -3, ...

Read More

Convert list of string to list of list in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 1K+ Views

In this article we will see how to convert a list of strings that represent lists into actual nested lists. This is useful when you have string representations of lists and need to convert them back to proper list structures. Using strip() and split() The strip() method removes the square brackets, while split() separates the elements by comma and space ? Example string_list = ['[0, 1, 2, 3]', '["Mon", "Tue", "Wed", "Thu"]'] print("The given list is:") print(string_list) print() # using strip() + split() result = [item.strip("[]").split(", ") for item in string_list] print("Converting list of ...

Read More

Python - Clearing list as dictionary value

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 208 Views

In Python, you may have a dictionary where values are lists and need to clear all the list values while keeping the keys. There are two main approaches: using the clear() method to empty existing lists in-place, or using dictionary comprehension to assign new empty lists. Using Loop with clear() Method The clear() method empties existing lists in-place, preserving the original list objects ? fruits = {"Apple": [4, 6, 9, 2], "Grape": [7, 8, 2, 1], "Orange": [3, 6, 2, 4]} print("Original dictionary:", fruits) # Clear each list in-place for key in fruits: ...

Read More

Python - Check if frequencies of all characters of a string are different

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 265 Views

In this article, we will see how to find the frequency of each character in a given string and check if all characters have different frequencies. We'll accomplish this in two steps: first calculating character frequencies, then checking if all frequencies are unique. Finding Character Frequencies We can count character frequencies using a dictionary. For each character in the string, we either increment its count or initialize it to 1 ? Example in_string = "She sells sea shells" char_freq = {} for char in in_string: if char in char_freq.keys(): ...

Read More

Python - Check if dictionary is empty

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 6K+ Views

During analysis of data sets we may come across situations where we have to deal with empty dictionaries. In this article we will see how to check if a dictionary is empty or not. Using if Statement The if condition evaluates to True if the dictionary has elements. Otherwise it evaluates to False. This is the most Pythonic way to check dictionary emptiness ? Example dict1 = {1: "Mon", 2: "Tue", 3: "Wed"} dict2 = {} # Given dictionaries print("The original dictionary : ", (dict1)) print("The original dictionary : ", (dict2)) # Check ...

Read More

Possible Words using given characters in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 917 Views

In this article, we'll see how to find possible words that can be formed using a given set of characters. We'll take a list of reference words and a list of available characters, then determine which words can be made from those characters. The program uses two functions: one to count character frequencies, and another to check if each word can be formed from the available characters. Example Here's how to find words that can be formed from given characters: def count_characters(character): char_count = {} for n ...

Read More
Showing 201–210 of 21,091 articles
« Prev 1 19 20 21 22 23 2110 Next »
Advertisements