Articles on Trending Technologies

Technical articles with clear explanations and examples

Python program to find the String in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 729 Views

When it is required to find the string in a list, a simple 'if' condition along with 'in' operator can be used. Python provides several approaches to check if a string exists in a list. Using the 'in' Operator The most straightforward way is using the 'in' operator, which returns True if the string is found ? my_list = [4, 3.0, 'python', 'is', 'fun'] print("The list is :") print(my_list) key = 'fun' print("The key is :") print(key) print("The result is :") if key in my_list: print("The key is present in ...

Read More

Python program for most frequent word in Strings List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 5K+ Views

When working with lists of strings, you may need to find the most frequent word across all strings. Python provides several approaches to solve this problem efficiently using collections and built-in functions. Using defaultdict The defaultdict from the collections module automatically initializes missing keys with a default value, making word counting straightforward ? from collections import defaultdict sentences = ["python is best for coders", "python is fun", "python is easy to learn"] print("The list is:") print(sentences) word_count = defaultdict(int) for sentence in sentences: for word in sentence.split(): ...

Read More

Python program to get maximum of each key Dictionary List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 397 Views

When working with a list of dictionaries, you often need to find the maximum value for each key across all dictionaries. Python provides several approaches to accomplish this task efficiently. Using Simple Iteration The most straightforward approach uses nested loops to iterate through each dictionary and track the maximum value for each key − my_list = [{"Hi": 18, "there": 13, "Will": 89}, {"Hi": 53, "there": 190, "Will": 87}] print("The list is:") print(my_list) my_result = {} for elem in my_list: ...

Read More

Python program to count the pairs of reverse strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 301 Views

When it is required to count the pairs of reverse strings, we need to check if each string in a list has its reverse counterpart present. A simple iteration is used to compare strings with their reversed versions. Example Below is a demonstration of counting reverse string pairs − def count_reverse_pairs(string_list): count = 0 visited = set() for i in range(len(string_list)): if i in visited: ...

Read More

Python program to print elements which are multiples of elements given in a list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 463 Views

When we need to find elements that are multiples of all elements in a given list, we can use list comprehension with the all() function. This approach efficiently filters elements that satisfy the multiple condition for every element in the divisor list. Example Below is a demonstration of finding multiples using list comprehension − numbers = [45, 67, 89, 90, 10, 98, 10, 12, 23] print("The list is:") print(numbers) divisors = [6, 4] print("The division list is:") print(divisors) multiples = [element for element in numbers if all(element % j == 0 for j ...

Read More

Python - Round number of places after the decimal for column values in a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 652 Views

To round the number of decimal places displayed for column values in a Pandas DataFrame, you can use the display.precision option. This controls how floating-point numbers are displayed without modifying the underlying data. Setting Display Precision First, import the required Pandas library − import pandas as pd Create a DataFrame with decimal values − import pandas as pd dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000] }) print("Original DataFrame:") print(dataFrame) ...

Read More

Python Program to print strings based on the list of prefix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 558 Views

When it is required to print strings based on the list of prefix elements, a list comprehension, the any() operator and the startswith() method are used. Example Below is a demonstration of the same ? my_list = ["streek", "greet", "meet", "leeks", "mean"] print("The list is:") print(my_list) prefix_list = ["st", "ge", "me", "re"] print("The prefix list is:") print(prefix_list) my_result = [element for element in my_list if any(element.startswith(ele) for ele in prefix_list)] print("The result is:") print(my_result) Output The list is: ['streek', 'greet', 'meet', 'leeks', 'mean'] The prefix list is: ['st', ...

Read More

Python Pandas - Convert Nested Dictionary to Multiindex Dataframe

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

Converting a nested dictionary to a Pandas MultiIndex DataFrame involves restructuring the dictionary keys as hierarchical column indices. This creates a DataFrame with multi-level column headers that represent the nested structure. Creating a Nested Dictionary First, let's create a nested dictionary with sports data − import pandas as pd # Create nested dictionary nested_dict = { 'Cricket': { 'Boards': ['BCCI', 'CA', 'ECB'], 'Country': ['India', 'Australia', 'England'] }, ...

Read More

Python prorgam to remove duplicate elements index from other list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 382 Views

When working with two lists, you may need to remove elements from the second list at positions where duplicate values occur in the first list. This can be accomplished using enumerate(), sets for tracking duplicates, and list comprehension. Problem Understanding Given two lists of the same length, we want to: Find duplicate elements in the first list Get the indices where these duplicates occur Remove elements from the second list at those duplicate indices Example Here's how to remove elements from the second list based on duplicate indices from the first list − ...

Read More

Python Program to remove a specific digit from every element of the list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 686 Views

When you need to remove a specific digit from every element of a list, you can use string manipulation with list comprehension and filtering techniques. Example Below is a demonstration of removing digit 3 from all list elements ? numbers = [123, 565, 1948, 334, 4598] print("The list is:") print(numbers) digit_to_remove = 3 print("The digit to remove is:") print(digit_to_remove) result = [] for element in numbers: # Convert to string and filter out the specific digit filtered_digits = ''.join([digit for digit in str(element) if ...

Read More
Showing 3681–3690 of 61,299 articles
« Prev 1 367 368 369 370 371 6130 Next »
Advertisements