Programming Articles

Page 326 of 2547

Python – Limit the values to keys in a Dictionary List

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

When working with a list of dictionaries, you often need to find the minimum and maximum values for each key across all dictionaries. Python provides the min() and max() functions to efficiently limit values to their bounds. Example Below is a demonstration of finding min/max values for each key ? my_list = [{"python": 4, "is": 7, "best": 10}, {"python": 2, "is": 5, "best": 9}, {"python": 1, "is": 2, "best": 6}] print("The list is ...

Read More

Python – Find the distance between first and last even elements in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 494 Views

When it is required to find the distance between the first and last even elements of a list, we can use list comprehension to find indices of even numbers and calculate the difference between the first and last positions. Example Below is a demonstration of finding the distance between first and last even elements ? my_list = [2, 3, 6, 4, 6, 2, 9, 1, 14, 11] print("The list is :") print(my_list) # Find indices of all even elements my_indices_list = [idx for idx in range(len(my_list)) if my_list[idx] % 2 == 0] # ...

Read More

Python – Filter rows with only Alphabets from List of Lists

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 548 Views

When working with a list of lists containing strings, you may need to filter rows that contain only alphabetic characters. Python's isalpha() method combined with list comprehension provides an efficient solution. Understanding isalpha() The isalpha() method returns True if all characters in a string are alphabetic (a-z, A-Z) and there is at least one character. It returns False if the string contains numbers, spaces, or special characters. Example Here's how to filter rows containing only alphabetic strings: data = [["python", "is", "best"], ["abc123", "good"], ["abc def ghij"], ["abc2", "gpqr"]] print("Original list:") print(data) ...

Read More

Python – Find the sum of Length of Strings at given indices

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 348 Views

When it is required to find the sum of the length of strings at specific indices, we can use various approaches. The enumerate function helps iterate through elements with their indices, allowing us to check if an index matches our target indices. Method 1: Using enumerate() This approach iterates through the list with indices and checks if each index is in our target list ? my_list = ["python", "is", "best", "for", "coders"] print("The list is :") print(my_list) index_list = [0, 1, 4] print("Target indices:", index_list) result = 0 for index, element in enumerate(my_list): ...

Read More

Python – Test if elements of list are in Min/Max range from other list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 628 Views

When it is required to test if all elements from one list fall within the minimum and maximum range of another list, we can iterate through the elements and check if each one lies between the min and max values. Example Below is a demonstration of the same − my_list = [5, 6, 4, 7, 8, 13, 15] print("The list is:") print(my_list) range_list = [4, 7, 10, 6] print("The range list is:") print(range_list) # Find min and max values from the main list min_val = min(my_list) max_val = max(my_list) print(f"Min value: {min_val}, Max value: ...

Read More

Python – Mean deviation of Elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 884 Views

When finding the mean deviation (standard deviation) of elements in a list, Python's sum() and len() functions are commonly used. Mean deviation measures how spread out the values are from the average. What is Mean Deviation? Mean deviation, often called standard deviation, is calculated by: Finding the mean (average) of all values Computing the squared differences from the mean Taking the average of those squared differences Finding the square root of that average Example Below is a demonstration of calculating mean deviation ? numbers = [3, 5, 7, 10, 12] ...

Read More

Find Rolling Mean – Python Pandas

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 406 Views

To find the rolling mean in Pandas, we use the rolling() method combined with mean(). This calculates the average of values within a sliding window. Let's explore different approaches to compute rolling means. Basic Setup First, import pandas and create a sample DataFrame ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({ "Car": ['Tesla', 'Mercedes', 'Tesla', 'Mustang', 'Mercedes', 'Mustang'], "Reg_Price": [5000, 1500, 6500, 8000, 9000, 6000] }) print("DataFrame:") print(dataFrame) DataFrame: Car Reg_Price ...

Read More

How to delete a column from Pandas DataFrame

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

To delete a column from a Pandas DataFrame, you have several methods available: del statement, drop() method, and pop() method. Each approach has its own use cases and advantages. Using del Statement The del statement permanently removes a column from the DataFrame ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90], "Price": [50000, 60000, 45000, 40000, 80000, 70000] }) print("Original DataFrame:") print(dataFrame) # Delete a ...

Read More

Python – Sort Strings by Case difference

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 225 Views

When working with strings in Python, you may need to sort them based on the case difference — the absolute difference between uppercase and lowercase letters. This technique uses custom sorting with the sort() method and a key function that calculates case differences. Understanding Case Difference Case difference is calculated by counting uppercase and lowercase letters in each string, then finding their absolute difference. Strings with smaller case differences are considered "more balanced" and appear first when sorted. Example Here's how to sort strings by case difference using a custom function ? def get_diff(my_string): ...

Read More

Python – Filter Similar Case Strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 285 Views

When it is required to filter similar case strings, list comprehension can be used along with isupper() and islower() methods. This technique helps identify strings that are either completely uppercase or completely lowercase, filtering out mixed-case strings. Example Below is a demonstration of filtering strings with consistent casing ? my_list = ["Python", "good", "FOr", "few", "CODERS"] print("The list is :") print(my_list) my_result = [sub for sub in my_list if sub.islower() or sub.isupper()] print("The strings with same case are :") print(my_result) Output The list is : ['Python', 'good', 'FOr', 'few', ...

Read More
Showing 3251–3260 of 25,466 articles
« Prev 1 324 325 326 327 328 2547 Next »
Advertisements