Programming Articles

Page 910 of 2547

Delete elements in range in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 894 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 590 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 222 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 290 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 930 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

Accessing all elements at given Python list of indexes

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

Sometimes we need to access multiple elements from a list at specific index positions. Python provides several efficient approaches to extract elements at given indices from a list. Using List Comprehension The most Pythonic approach uses list comprehension to iterate through the index list and extract corresponding elements ? days = ["Mon", "Tue", "Wed", "Thu", "Fri"] indices = [1, 3, 4] # printing the lists print("Given list: " + str(days)) print("List of indices: " + str(indices)) # use list comprehension result = [days[i] for i in indices] # Get the result print("Result list: ...

Read More

Python - Convert column to separate elements in list of lists

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

When working with data structures in Python, you often need to reshape lists by separating columns into different elements. This is particularly useful when converting tabular data into different formats or extracting specific column ranges from nested lists. Using List Slicing and Comprehension You can slice lists at specific positions to create a columnar structure. This approach splits each sublist into two parts: elements from index 2 onwards and elements from index 0 to 2 ? Example data = [[5, 10, 15, 20], [25, 30, 35, 40], [45, 50, 55, 60]] print("The given input ...

Read More
Showing 9091–9100 of 25,466 articles
« Prev 1 908 909 910 911 912 2547 Next »
Advertisements