Programming Articles

Page 328 of 2547

Python - Selective consecutive Suffix Join

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 207 Views

When working with lists of strings, you may need to group consecutive elements that end with a specific suffix. This technique uses simple iteration with the endswith() method to join elements until a non-suffix element is found. Example Below is a demonstration of selective consecutive suffix join ? my_list = ["Python-", "fun", "to-", "code"] print("The list is :") print(my_list) suffix = '-' print("The suffix is :") print(suffix) result = [] temp = [] for element in my_list: temp.append(element) if not ...

Read More

Python – Center align column headers of a Pandas DataFrame

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

To center align column headers in a Pandas DataFrame, use the display.colheader_justify option with center value. This setting affects how column headers are displayed when printing DataFrames. Syntax pd.set_option('display.colheader_justify', 'center') Creating a DataFrame First, let's create a sample DataFrame with car data — import pandas as pd # Create DataFrame car_data = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000] } ) print("DataFrame with ...

Read More

Python Program that print elements common at specified index of list elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 540 Views

When working with a list of strings, you might need to find characters that appear at the same position across all strings. This can be achieved using list comprehension, the min() function, and a Boolean flag to track common characters. Example Below is a demonstration of finding common characters at each index position − my_list = ["week", "seek", "beek", "reek", 'meek', 'peek'] print("The list is :") print(my_list) min_length = min(len(element) for element in my_list) my_result = [] for index in range(0, min_length): flag = True for ...

Read More

Python Program to print element with maximum vowels from a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 726 Views

When it is required to print element with maximum vowels from a list, we can iterate through each word and count vowels using list comprehension or other methods. Example Below is a demonstration of the same − words = ["this", "week", "is", "going", "great"] print("The list is :") print(words) result = "" max_vowel_count = 0 vowels = ['a', 'e', 'i', 'o', 'u'] for word in words: vowel_count = len([char for char in word.lower() if char in vowels]) if vowel_count > max_vowel_count: ...

Read More

Python program to find the String in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 608 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 304 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 243 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 386 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 586 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
Showing 3271–3280 of 25,466 articles
« Prev 1 326 327 328 329 330 2547 Next »
Advertisements