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 replace all Characters of a List except the given character
When it is required to replace all characters of a list except a given character, a list comprehension and the == operator are used. This technique allows you to preserve specific characters while replacing all others with a substitute character. Using List Comprehension The most Pythonic approach uses a conditional list comprehension to check each element ? characters = ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'H', 'P'] print("The list is:") print(characters) replace_char = '$' retain_char = 'P' result = [element if element == retain_char else replace_char for element in characters] print("The ...
Read MorePython program to concatenate Strings around K
String concatenation around a delimiter involves combining strings that appear before and after a specific character K. This technique is useful for merging related string elements separated by a common delimiter. Syntax The general approach uses iteration to identify patterns where strings are separated by the delimiter K ? while index < len(list): if list[index + 1] == K: combined = list[index] + K + list[index + 2] index += 2 result.append(element) ...
Read MorePython program for sum of consecutive numbers with overlapping in lists
When summing consecutive numbers with overlapping elements in lists, we create pairs of adjacent elements where each element (except the last) is paired with its next neighbor. The last element pairs with the first element to create a circular pattern. Example Below is a demonstration using list comprehension with zip ? my_list = [41, 27, 53, 12, 29, 32, 16] print("The list is :") print(my_list) my_result = [a + b for a, b in zip(my_list, my_list[1:] + [my_list[0]])] print("The result is :") print(my_result) Output The list is : [41, ...
Read MorePython program to remove rows with duplicate element in Matrix
When it is required to remove rows with duplicate elements in a matrix, a list comprehension and the set operator is used to filter out rows containing duplicate values. Understanding the Problem A matrix (list of lists) may contain rows where elements are repeated. We need to identify and remove such rows, keeping only those with unique elements ? # Example matrix with some rows having duplicate elements matrix = [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] print("Original matrix:") for i, row in enumerate(matrix): print(f"Row {i}: ...
Read MorePython 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 More