Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Python program to sort strings by substring range
Sorting strings by substring range allows you to order a list based on specific character positions within each string. Python provides an efficient way to achieve this using the sort() method with a custom key function. Basic Example Here's how to sort strings based on characters at positions 1 to 3 − def get_substring(my_string): return my_string[1:3] words = ["python", "is", "fun", "to", "learn"] print("Original list:") print(words) print("Substring range: positions 1-3") print("Substrings used for sorting:") for word in words: print(f"'{word}' → '{word[1:3]}'") words.sort(key=get_substring) print("Sorted ...
Read MorePython – Extract String elements from Mixed Matrix
When working with mixed matrices containing different data types, you often need to extract only string elements. Python provides the isinstance() function to check data types, which can be combined with list comprehension for efficient filtering. Understanding Mixed Matrices A mixed matrix is a nested list containing elements of different data types like integers, strings, and floats in the same structure. Using isinstance() with List Comprehension The most efficient approach is to use list comprehension with isinstance() to filter string elements ? my_list = [[35, 66, 31], ["python", 13, "is"], [15, "fun", 14]] ...
Read MorePython program to replace first 'K' elements by 'N'
When it is required to replace first 'K' elements by 'N', a simple iteration is used. This operation modifies the original list by substituting the first K elements with a specified value N. Basic Approach Using Loop The simplest method uses a for loop with range() to iterate through the first K positions ? my_list = [13, 34, 26, 58, 14, 32, 16, 89] print("The list is :") print(my_list) K = 2 print("The value of K is :") print(K) N = 99 print("The value of N is :") print(N) for index in ...
Read MorePython – Insert character in each duplicate string after every K elements
When it is required to insert a character in each duplicate string after every K elements, a method is defined that uses the append method, concatenation operator and list slicing to create multiple versions of the string with the character inserted at different positions. Example Below is a demonstration of the same − def insert_char_after_key_elem(my_string, my_key, my_char): my_result = [] for index in range(0, len(my_string), my_key): my_result.append(my_string[:index] + my_char + my_string[index:]) return my_result my_string = ...
Read MorePython – Average of digit greater than K
When it is required to find the average of numbers greater than K in a list, we can use iteration to filter elements and calculate the mean. This involves counting qualifying elements and summing their values. Example my_list = [11, 17, 25, 16, 23, 18] print("The list is :") print(my_list) K = 15 print("The value of K is") print(K) my_sum = 0 my_count = 0 for number in my_list: if number > K: my_sum += number ...
Read MorePython – Next N elements from K value
When working with lists in Python, you might need to extract the next N elements starting from a specific position K. This operation is useful for data processing and list manipulation tasks. Understanding the Problem Given a list, a starting position K, and a count N, we want to get the next N elements from position K onwards. This means extracting elements from index K to index K+N-1. Method 1: Using List Slicing The most Pythonic way to get next N elements from position K is using list slicing ? my_list = [31, 24, ...
Read MorePython – Remove Tuples with difference greater than K
When working with tuples, you may need to filter out tuples where the absolute difference between elements exceeds a threshold value K. Python provides an efficient way to accomplish this using list comprehension and the abs() function. Example Here's how to remove tuples with difference greater than K ? my_tuple = [(41, 18), (21, 57), (39, 22), (23, 42), (22, 10)] print("The tuple is :") print(my_tuple) K = 20 my_result = [element for element in my_tuple if abs(element[0] - element[1])
Read MorePython – Remove dictionary from a list of dictionaries if a particular value is not present
When you need to remove a dictionary from a list based on whether a particular value is present or absent, you can use several approaches. The most common methods are iteration with del, list comprehension, or the filter() function. Method 1: Using del with Index-based Iteration This method iterates through the list and removes the dictionary when a condition is met ? my_list = [{"id": 1, "data": "Python"}, {"id": 2, "data": "Code"}, {"id": 3, ...
Read MorePython – Extract Strings with Successive Alphabets in Alphabetical Order
When working with strings, you may need to extract strings that contain successive alphabets in alphabetical order. This means finding strings where at least two consecutive characters follow each other in the alphabet (like 'hi' where 'h' and 'i' are consecutive). Understanding Successive Alphabets Successive alphabets are characters that follow each other in the alphabet sequence. For example: 'ab' - 'a' and 'b' are successive 'hi' - 'h' and 'i' are successive 'xyz' - 'x', 'y', 'z' are all successive Method: Using ord() Function The ord() function returns the Unicode code point of a ...
Read MorePython – Test for desired String Lengths
When it is required to test for desired string lengths, a simple iteration and the len() method can be used. This technique helps verify that each string in a list matches its corresponding expected length. Using Basic Iteration Below is a demonstration of checking string lengths using a for loop ? strings = ["python", "is", "fun", "to", "learn", "Will", "how"] print("The list is :") print(strings) expected_lengths = [6, 2, 3, 2, 5, 4, 3] result = True for index in range(len(strings)): if len(strings[index]) != expected_lengths[index]: ...
Read More