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
Python Articles
Page 520 of 855
Removing strings from tuple in Python
When it is required to remove strings from a tuple, the list comprehension and the type() method can be used. This approach allows you to filter out string elements while preserving other data types like integers and floats. A list can be used to store heterogeneous values (data of any data type like integer, floating point, strings, and so on). A list of tuples contains tuples enclosed in a list. List comprehension is a shorthand to iterate through the list and perform operations on it. The type() method returns the class of the object passed to it as ...
Read MoreGrouped summation of tuple list in Python
When working with lists of tuples representing key-value pairs, you often need to group them by keys and sum their values. Python's Counter from the collections module provides an elegant solution for this task. The Counter is a specialized dictionary subclass that counts hashable objects. It creates a hash table automatically when initialized with an iterable, making it perfect for aggregating values by keys. Using Counter for Grouped Summation Here's how to sum values for matching keys across multiple tuple lists ? from collections import Counter my_list_1 = [('Hi', 14), ('there', 16), ('Jane', 28)] ...
Read MoreReverse each tuple in a list of tuples in Python
When it is required to reverse each tuple in a list of tuples, the negative step slicing can be used. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). A list of tuple basically contains tuples enclosed in a list. In negative slicing, the index is accessed using negative numbers, instead of positive ones. The slice notation [::-1] reverses the order of elements in each tuple. Using List Comprehension with Negative Slicing Below is a demonstration for the same − ...
Read MorePython program to check whether the string is Symmetrical or Palindrome
When checking if a string is symmetrical or palindromic, we need to understand two different concepts. A palindrome reads the same forwards and backwards (like "racecar"), while a symmetrical string has identical first and second halves (like "abcabc"). Understanding Palindromes vs Symmetrical Strings A palindrome compares characters from both ends moving inward, while symmetry compares the first half with the second half of the string. Complete Example Here's a program that demonstrates both palindrome and symmetry checking ? def check_palindrome(my_str): mid_val = (len(my_str)-1)//2 start = 0 ...
Read MorePython Program to Determine How Many Times a Given Letter Occurs in a String Recursively
When counting how many times a specific letter appears in a string, we can use recursion to break down the problem into smaller subproblems. Recursion solves the bigger problem by computing results from smaller parts and combining them together. Recursive Approach The recursive function checks each character one by one. If the current character matches our target, we count it and continue with the rest of the string ? def count_letter_recursive(text, target_letter): # Base case: empty string if not text: ...
Read MorePython Program to Determine Whether a Given Number is Even or Odd Recursively
When determining whether a number is even or odd, we typically use the modulo operator. However, we can also solve this problem recursively by repeatedly subtracting 2 until we reach a base case. Recursion breaks down the problem into smaller subproblems. Here, we reduce the number by 2 in each recursive call until we reach 0 (even) or 1 (odd). How the Recursive Approach Works The algorithm works by: Base case: If the number is less than 2 (0 or 1), check if it's divisible by 2 Recursive case: Subtract 2 from the number and ...
Read MorePython Program that Displays which Letters are in the Two Strings but not in Both
When working with two strings, we often need to find letters that appear in one string but not in both. Python's set operations provide an elegant solution using the symmetric difference operator (^). A list can store heterogeneous values (integers, strings, floats, etc.). The list() function converts any iterable to a list type. Python's set data type contains only unique elements. Sets are useful for operations like intersection, difference, union, and symmetric difference. Syntax set1 ^ set2 # Symmetric difference - elements in either set, but not both Example Below is ...
Read MorePython Program that Displays which Letters are in the First String but not in the Second
When it is required to display the letters that are present in the first string but not in the second string, we can use Python's set data structure to find the difference between two strings efficiently. Python's set contains unique elements only and is useful for performing operations such as intersection, difference, union, and symmetric difference. Using Set Difference The most efficient approach is to convert both strings to sets and use the difference operator - ? # Define two strings first_string = "programming" second_string = "coding" # Find letters in first string but ...
Read MorePython Program to Create a Dictionary with Key as First Character and Value as Words Starting with that Character
When you need to group words by their first character, you can create a dictionary where keys are first characters and values are lists of words starting with that character. This uses the split() method, dictionary operations, and conditional logic. Example Below is a demonstration for the same − my_string = "Hey Jane how are you doing today" split_string = my_string.split() my_dict = {} for elem in split_string: if elem[0] not in my_dict.keys(): my_dict[elem[0]] = [] ...
Read MorePython Program to Form a Dictionary from an Object of a Class
When working with classes in Python, you often need to convert an object's attributes into a dictionary format. Python provides the __dict__ attribute that automatically creates a dictionary containing all instance attributes of an object. Basic Example Here's how to create a dictionary from a class object using the __dict__ attribute ? class BaseClass: def __init__(self): self.A = 32 self.B = 60 my_instance = BaseClass() print("An instance of the class has been created") print(my_instance.__dict__) ...
Read More