Server Side Programming Articles

Page 105 of 2109

Group Elements in Matrix using Python

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 487 Views

Matrices are widely used in various fields, including mathematics, physics, and computer science. In some situations, we need to group elements of the matrix based on certain criteria. We can group elements of the matrix by row, column, values, condition, etc. In this article, we will understand how we can group elements of the matrix using Python. Creating a Matrix Before diving into the grouping methods, we can start by creating a matrix in Python. We can use the NumPy library to work with matrices efficiently. Here's how we can create a matrix using NumPy ? ...

Read More

Get the summation of numbers in a string list using Python

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 1K+ Views

In Python, we can sum the numbers present in a string list using various methods like Regular Expressions, isdigit() with List Comprehension, and filtering techniques. When working with lists containing strings, you often need to extract and sum numerical values from those strings. This is particularly useful when processing data from text files, log files, or web scraping. Algorithm A general algorithm to find the sum of numbers in a string list using Python is as follows: Define a function that takes a string list as input Join all elements of the string list into a ...

Read More

Get the Number of Characters, Words, Spaces, and Lines in a File using Python

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 13K+ Views

Text file analysis is a fundamental task in various data processing and natural language processing applications. Python provides numerous built-in features and libraries to facilitate such tasks efficiently. In this article, we will explore how to count the number of characters, words, spaces, and lines in a text file using Python. Method 1: Manual Count Method In this method, we will develop our own logic to read a text file and count the number of characters, words, spaces, and lines without using specialized built-in methods. Algorithm Open the file in read mode using the open() ...

Read More

Get the indices of Uppercase Characters in a given String using Python

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 3K+ Views

In Python, we can get the indices of uppercase characters in a given string using methods like list comprehension, for loops, and regular expressions. Finding the indices of uppercase characters can be useful in text analysis and manipulation tasks. Method 1: Using List Comprehension List comprehension provides a concise and efficient way to iterate through characters, check if they are uppercase using the isupper() method, and store their indices in a list ? Syntax indices = [index for index, char in enumerate(string) if char.isupper()] The enumerate() function returns both the index and character ...

Read More

Filter Non-None dictionary keys in Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 902 Views

Filtering Non-None dictionary keys in Python is a common task when working with data that contains missing or empty values. Python provides several efficient methods to remove None values from dictionaries using built-in functions like items(), filter(), and dictionary comprehensions. Let's explore different approaches to transform a dictionary like {'A': 11, 'B': None, 'C': 29, 'D': None} into {'A': 11, 'C': 29}. Using Dictionary Comprehension Dictionary comprehension provides the most readable and Pythonic way to filter None values ? def filter_none(dictionary): return {key: value for key, value in dictionary.items() if value ...

Read More

How to Filter list elements starting with a given Prefix using Python?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 1K+ Views

Filtering list elements by prefix is a common task in Python programming. We can use several approaches including list comprehension, for loops, filter() function, or string slicing to accomplish this task. Let's understand with an example ? # Example: Filter names starting with "Am" names = ["Amelia", "Kinshuk", "Rosy", "Aman"] prefix = "Am" result = [name for name in names if name.startswith(prefix)] print(result) ['Amelia', 'Aman'] Key Methods Used startswith() Method The startswith() method returns True if a string starts with the specified prefix ? text = "Python programming" ...

Read More

Python - Filter odd elements from value lists in dictionary

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 978 Views

Python dictionaries store key-value pairs where values can be lists. Filtering odd elements from these lists is a common task in data processing. An odd number is any integer that gives a remainder when divided by 2 (x % 2 != 0). For example − Given dictionary: {'A': [10, 21, 22, 19], 'B': [2, 5, 8]} After filtering odd elements: {'A': [21, 19], 'B': [5]} Understanding Odd Number Detection To identify odd numbers, we use the modulo operator ? # Two ways to check for odd numbers numbers = [1, 2, 3, 4, ...

Read More

Filter Range Length Tuples in Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 731 Views

Filtering tuples based on their length within a specific range is a common data processing task in Python. You can filter a list of tuples to keep only those with lengths between minimum and maximum values using various approaches like generator expressions, list comprehensions, loops, or filter functions. Using Generator Expression Generator expressions provide memory-efficient filtering by creating an iterator instead of storing all filtered results in memory ? def filter_range_tuples(tuples, min_len, max_len): return tuple(t for t in tuples if min_len

Read More

Convert Lists to Nested Dictionary in Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 1K+ Views

A nested dictionary is a dictionary that contains other dictionaries as values. Converting lists to nested dictionaries is useful for organizing hierarchical data structures. Python provides several approaches including loops, the reduce() function, dictionary comprehension, and recursion. Using for Loop The simplest approach uses a for loop to iterate through a list and create nested dictionaries ? my_list = ['a', 'b', 'c'] # Create empty dictionary and reference for nesting emp_dict = p_list = {} for item in my_list: p_list[item] = {} p_list = p_list[item] ...

Read More

How to select a range of rows from a dataframe in PySpark?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 1K+ Views

A PySpark DataFrame is a distributed collection of data organized into rows and columns. Selecting a range of rows means filtering data based on specific conditions. PySpark provides several methods like filter(), where(), and collect() to achieve this. Setting Up PySpark First, install PySpark and import the required modules ? pip install pyspark from pyspark.sql import SparkSession # Create SparkSession spark = SparkSession.builder \ .appName('DataFrame_Range_Selection') \ .getOrCreate() # Sample data customer_data = [ ("PREM KUMAR", 1281, "AC", 40000, 4000), ...

Read More
Showing 1041–1050 of 21,090 articles
« Prev 1 103 104 105 106 107 2109 Next »
Advertisements