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 – Mapping Matrix with Dictionary
When working with matrices (lists of lists) in Python, you often need to map numerical values to meaningful strings using a dictionary. This technique is useful for converting coded data into readable format. Basic Matrix Mapping Here's how to map matrix elements using a dictionary with nested loops − my_matrix = [[2, 4, 3], [4, 1, 3], [2, 1, 3, 4]] print("Original matrix:") print(my_matrix) # Mapping dictionary map_dict = {2: "Python", 1: "fun", 3: "to", 4: "learn"} # Map matrix elements result = [] for row in my_matrix: mapped_row ...
Read MorePython – Convert Integer Matrix to String Matrix
When working with matrices in Python, you may need to convert an integer matrix to a string matrix for display purposes or string operations. Python provides several approaches to accomplish this conversion efficiently. Using List Comprehension The most pythonic way is using nested list comprehension to iterate through the matrix and convert each element ? matrix = [[14, 25, 17], [40, 28, 13], [59, 44, 66], [29, 33, 16]] print("Original integer matrix:") print(matrix) # Convert each integer to string using nested list comprehension string_matrix = [[str(element) for element in row] for row in matrix] ...
Read MorePython program to extract characters in given range from a string list
When it is required to extract characters in a given range from a string list, we can use list comprehension and string slicing. This technique allows us to join all strings and extract a specific portion based on character positions. Example Below is a demonstration of extracting characters from position 11 to 25 ? my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) start, end = 11, 25 my_result = ''.join([element for element in my_list])[start : end] print("The result is :") print(my_result) Output The list ...
Read MorePython – Sort a List by Factor count
When you need to sort a list by the number of factors each element has, you can use a custom function with Python's sort() method. This technique uses list comprehension and the modulus operator to count factors efficiently. Understanding Factor Count A factor of a number is any integer that divides the number evenly (remainder is 0). For example, the factors of 12 are: 1, 2, 3, 4, 6, and 12. Example Here's how to sort a list by factor count ? def factor_count(element): return len([element for index in range(1, ...
Read MorePython – Random range in a List
When you need to generate a list of random numbers within a specific range, Python's random.randrange() method combined with list comprehension provides an efficient solution. Syntax The random.randrange() function generates random integers within a specified range ? random.randrange(start, stop, step) start − Starting value (inclusive) stop − Ending value (exclusive) step − Step size (optional, default is 1) Basic Example Generate 10 random numbers between 1 and 100 ? import random my_result = [random.randrange(1, 100, 1) for i in range(10)] print("The result is :") print(my_result) ...
Read MorePython – Extract Sorted Strings
When you need to filter strings that have their characters in sorted order, you can use list comprehension with the sorted() method. A sorted string is one where the characters appear in alphabetical order. Example Below is a demonstration of extracting sorted strings from a list − my_list = ["pyt", "Fdf", "Fun"] print("The list is :") print(my_list) my_result = [element for element in my_list if ''.join(sorted(element)) == element] print("The result is :") print(my_result) Output The list is : ['pyt', 'Fdf', 'Fun'] The result is : ['Fdf'] How ...
Read MorePython - Merge a Matrix by the Elements of First Column
When it is required to merge a matrix by the elements of first column, a simple iteration and list comprehension with setdefault method is used. This technique groups rows that have the same value in their first column. Example Below is a demonstration of the same — my_list = [[41, "python"], [13, "pyt"], [41, "is"], [4, "always"], [3, "fun"]] print("The list is :") print(my_list) my_result = {} for key, value in my_list: my_result.setdefault(key, []).append(value) my_result = [[key] + value for key, value in my_result.items()] print("The result is :") ...
Read MorePython – Concatenate Strings in the Given Order
When you need to concatenate strings in a specific custom order, you can use simple iteration with index-based access. This technique allows you to rearrange and join string elements according to any desired sequence. Basic Example Below is a demonstration of concatenating strings in a custom order − my_list = ["pyt", "fun", "for", "learning"] print("The list is:") print(my_list) sort_order = [1, 0, 3, 2] my_result = '' for element in sort_order: my_result += my_list[element] print("The result is:") print(my_result) The list is: ['pyt', 'fun', 'for', 'learning'] ...
Read MorePython Program to remove elements that are less than K difference away in a list
When it is required to remove elements that are less than K difference away in a list, a simple iteration and 'if' condition is used. This technique ensures that any two consecutive elements in the final list have a difference of at least K. Example Below is a demonstration of the same − my_list = [13, 29, 24, 18, 40, 15] print("The list is :") print(my_list) K = 3 my_list = sorted(my_list) index = 0 while index < len(my_list) - 1: if my_list[index] + K > my_list[index ...
Read MorePython – Append List every Nth index
When you need to insert elements from one list at every Nth index position of another list, you can use enumerate() to track indices and conditionally insert elements. Basic Example Here's how to append a list every Nth index ? numbers = [13, 27, 48, 12, 21, 45, 28, 19, 63] append_list = ['P', 'Y', 'T'] N = 3 print("Original list:") print(numbers) print(f"Append list: {append_list}") print(f"N = {N}") result = [] for index, element in enumerate(numbers): if index % N == 0: ...
Read More