Articles on Trending Technologies

Technical articles with clear explanations and examples

Python – Test if list is Palindrome

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

A palindrome is a sequence that reads the same forwards and backwards. To test if a Python list is a palindrome, we can compare it with its reversed version using slice notation [::-1]. Method 1: Direct List Comparison The simplest approach is to compare the original list with its reverse ? def is_palindrome_list(data): return data == data[::-1] # Test with palindrome list numbers = [1, 2, 3, 2, 1] print("List:", numbers) print("Is palindrome:", is_palindrome_list(numbers)) # Test with non-palindrome list numbers2 = [1, 2, 3, 4, 5] print("List:", numbers2) print("Is palindrome:", ...

Read More

Python – Row with Minimum difference in extreme values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 169 Views

When it is required to get the row with minimum difference in extreme values, list comprehension, the min() method and max() methods are used. Example Below is a demonstration of the same − my_list = [[41, 1, 38], [25, 33, 1], [13, 44, 65], [1, 22]] print("The list is : ") print(my_list) my_min_val = min([max(elem) - min(elem) for elem in my_list]) my_result = [elem for elem in my_list if max(elem) - min(elem) == my_min_val] print("The result is : ") print(my_result) The output of the above code is − ...

Read More

Python - Filter Pandas DataFrame by Time

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 814 Views

Filtering a Pandas DataFrame by time allows you to extract records that meet specific date or time conditions. Use the loc indexer with datetime comparisons to filter rows based on date ranges. Basic Time Filtering with loc First, create a DataFrame with date information ? import pandas as pd # Create dictionary with car purchase data data = { 'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_Purchase': ['2021-07-10', '2021-08-12', '2021-06-17', '2021-03-16', '2021-05-19', '2021-08-22'] } # Create DataFrame dataFrame = pd.DataFrame(data) print("Original DataFrame:") print(dataFrame) ...

Read More

Python Program to Remove Palindromic Elements from a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 459 Views

When it is required to remove palindromic elements from a list, list comprehension and the 'not' operator are used. A palindromic number reads the same forwards and backwards, like 121 or 9. Example Below is a demonstration of removing palindromic elements from a list − my_list = [56, 78, 12, 32, 4, 8, 9, 100, 11] print("The list is:") print(my_list) my_result = [elem for elem in my_list if int(str(elem)[::-1]) not in my_list] print("The result is:") print(my_result) Output The list is: [56, 78, 12, 32, 4, 8, 9, 100, 11] ...

Read More

Python Pandas - Count the number of rows in each group

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 492 Views

Pandas groupby() operations allow you to split data into groups and count rows in each group using size(). This is useful for analyzing data distribution and finding group frequencies. Creating the DataFrame First, let's create a sample DataFrame with product data ? import pandas as pd # Create a DataFrame dataFrame = pd.DataFrame({ 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Quantity': [10, 50, 10, 20, 25, 50], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'] }) print("DataFrame:") print(dataFrame) ...

Read More

Python – N sized substrings with K distinct characters

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 400 Views

When working with strings, you might need to find all substrings of a specific length that contain exactly K distinct characters. This can be achieved by iterating through the string and using Python's set() method to count unique characters in each substring. Syntax The general approach involves: for i in range(len(string) - n + 1): substring = string[i:i+n] if len(set(substring)) == k: # Add to result Example Below is a demonstration that finds all 2-character substrings with ...

Read More

Python – Merge Dictionaries List with duplicate Keys

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 932 Views

When working with lists of dictionaries, you often need to merge them while handling duplicate keys. This process involves iterating through corresponding dictionaries and adding keys that don't already exist. Basic Dictionary List Merging Here's how to merge two lists of dictionaries, keeping original values for duplicate keys ? list1 = [{"aba": 1, "best": 4}, {"python": 10, "fun": 15}, {"scala": "fun"}] list2 = [{"scala": 6}, {"python": 3, "best": 10}, {"java": 1}] print("First list:") print(list1) print("Second list:") print(list2) # Merge dictionaries at corresponding positions for i in range(len(list1)): existing_keys = list(list1[i].keys()) ...

Read More

Python Pandas - Sort DataFrame in descending order according to the element frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 847 Views

To sort a Pandas DataFrame in descending order according to element frequency, you need to group the data, count occurrences, and use sort_values() with ascending=False. Basic Syntax The key is combining groupby(), count(), and sort_values() ? df.groupby(['column']).count().sort_values(['count_column'], ascending=False) Creating Sample Data Let's create a DataFrame with car data to demonstrate frequency sorting ? import pandas as pd # Create DataFrame with duplicate car entries dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Lexus'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 2000], ...

Read More

Python - Move a column to the first position in Pandas DataFrame?

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

Use pop() to remove a column and insert() to place it at the first position in a Pandas DataFrame. This technique allows you to reorder columns efficiently without creating a new DataFrame. Creating the DataFrame First, let's create a sample DataFrame with three columns ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'], ...

Read More

Python – Display only non-duplicate values from a DataFrame

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

In this tutorial, we will learn how to display only non-duplicate values from a Pandas DataFrame. We'll use the duplicated() method combined with the logical NOT operator (~) to filter out duplicate entries. Creating a DataFrame with Duplicates First, let's create a DataFrame containing duplicate values ? import pandas as pd # Create DataFrame with duplicate student names dataFrame = pd.DataFrame({ "Student": ['Jack', 'Robin', 'Ted', 'Robin', 'Scarlett', 'Kat', 'Ted'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'] }) print("Original DataFrame:") print(dataFrame) Original DataFrame: ...

Read More
Showing 3611–3620 of 61,299 articles
« Prev 1 360 361 362 363 364 6130 Next »
Advertisements