Python Articles

Page 138 of 855

Keys associated with Values in Dictionary in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 475 Views

When you need to find the keys associated with specific values in a dictionary, Python provides several approaches. The most common method is using the index() method with dictionary keys and values converted to lists. Using index() Method This approach converts dictionary keys and values to lists, then uses index() to find the position ? my_dict = {"Hi": 100, "there": 121, "Mark": 189} print("The dictionary is:") print(my_dict) dict_keys = list(my_dict.keys()) print("The keys in the dictionary are:") print(dict_keys) dict_values = list(my_dict.values()) print("The values in the dictionary are:") print(dict_values) # Find key for value 100 ...

Read More

Python dictionary, set and counter to check if frequencies can become same

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 405 Views

When working with character frequencies in strings, we often need to check if all characters can have the same frequency with minimal changes. This problem can be solved using Python's Counter from the collections module to count frequencies, then analyzing the frequency distribution. Understanding the Problem The goal is to determine if we can make all character frequencies equal by removing at most one character. For example, in "xxxyyyzzzzzz", we have frequencies [3, 3, 6] which cannot be made equal with just one removal. Solution Using Counter and Set Here's how we can solve this problem ...

Read More

Python counter and dictionary intersection example

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 361 Views

When working with string manipulation and character frequency analysis, the Counter class from the collections module provides an elegant way to count character occurrences. The intersection operation (&) between two Counter objects helps determine if one string's characters are a subset of another's. Understanding Counter Intersection The intersection of two Counter objects returns the minimum count of each common element. This is useful for checking if one string can be formed using characters from another string ? from collections import Counter def can_form_string(str_1, str_2): dict_one = Counter(str_1.lower()) ...

Read More

Python dictionary with keys having multiple inputs

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

When working with Python dictionaries, you can use tuples as keys to create dictionary entries where each key consists of multiple values. This is useful when you need to map combinations of values to specific results. Below is the demonstration of the same − Example my_dict = {} a, b, c = 15, 26, 38 my_dict[a, b, c] = a + b - c a, b, c = 5, 4, 11 my_dict[a, b, c] = a + b - c print("The dictionary is :") print(my_dict) The output of the above code ...

Read More

Check order of character in string using OrderedDict( ) in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 782 Views

When checking if characters in a string appear in the same order as a given pattern, the OrderedDict from Python's collections module provides an effective solution. This method preserves the order of characters as they first appear in the string. How OrderedDict Helps An OrderedDict maintains the insertion order of keys. Using OrderedDict.fromkeys() creates a dictionary with unique characters from the string in their first appearance order, which we can then compare against our pattern. Example from collections import OrderedDict def check_order(my_input, my_pattern): my_dict = OrderedDict.fromkeys(my_input) ...

Read More

Insertion at the beginning in OrderedDict using Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 584 Views

When working with OrderedDict in Python, you may need to insert elements at the beginning rather than the end. Python's OrderedDict doesn't have a direct method for insertion at the beginning, but you can achieve this using the update() method combined with move_to_end(). What is OrderedDict? OrderedDict is a dictionary subclass from the collections module that remembers the order in which items were inserted. This makes it useful when the sequence of key-value pairs matters. Method: Using update() and move_to_end() To insert at the beginning, first add the new item using update(), then move it to ...

Read More

Extract Unique dictionary values in Python Program

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

When it is required to extract unique values from a dictionary, there are several approaches available. The most effective method combines set comprehension with sorted() to eliminate duplicates and arrange values in order. Using Set Comprehension with Sorted This approach flattens all dictionary values into a set (removing duplicates) and then sorts them ? my_dict = {'hi': [5, 3, 8, 0], 'there': [22, 51, 63, 77], 'how': [7, 0, 22], ...

Read More

Count Number of Lowercase Characters in a String in Python Program

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 2K+ Views

When you need to count the number of lowercase characters in a string, Python provides the islower() method which can be combined with a simple for loop to achieve this task. Using islower() Method with for Loop The islower() method returns True if the character is a lowercase letter, and False otherwise ? my_string = "Hi there how are you" print("The string is") print(my_string) my_counter = 0 for i in my_string: if(i.islower()): my_counter = my_counter + 1 print("The number of lowercase characters ...

Read More

Take in Two Strings and Display the Larger String without Using Built-in Functions in Python Program

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 905 Views

When it is required to take two strings and display the larger string without using any built-in function, a counter can be used to get the length of the strings, and if condition can be used to compare their lengths. Below is the demonstration of the same − Example string_1 = "Hi there" string_2 = "Hi how are ya" print("The first string is :") print(string_1) print("The second string is :") print(string_2) count_1 = 0 count_2 = 0 for i in string_1: count_1 = count_1 + 1 for j in ...

Read More

Python Program to Find All Connected Components using DFS in an Undirected Graph

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 2K+ Views

When working with undirected graphs, connected components are subsets of vertices where each vertex is reachable from every other vertex within the same component. We can find all connected components using Depth-First Search (DFS) traversal. Below is a demonstration of finding connected components using a graph class ? Graph Class Implementation First, let's create a graph class with methods for DFS traversal and finding connected components ? class Graph_struct: def __init__(self, V): self.V = V ...

Read More
Showing 1371–1380 of 8,549 articles
« Prev 1 136 137 138 139 140 855 Next »
Advertisements