Programming Articles

Page 55 of 2547

Filter key from Nested item using Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 1K+ Views

Python can extract specific values from complex data structures containing nested dictionaries or lists by filtering keys from nested items. This technique is essential for data manipulation, API response processing, and configuration parsing where you need to work with specific parts of complex data structures. Key Methods The following built-in methods are commonly used for filtering nested data ? isinstance() Checks whether an object is an instance of a specific class or type ? data = {"name": "John"} print(isinstance(data, dict)) print(isinstance([1, 2, 3], list)) True True items() Returns key-value pairs ...

Read More

Python - Find Minimum Pair Sum in list

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 815 Views

The Minimum pair sum is defined by finding the smallest possible sum of two numbers taken from a given list. This concept is useful in optimization problems where you need to minimize costs, distances, or time for operations. Python provides several approaches to solve this using built-in functions like sort(), combinations(), and float(). Using Nested for Loop This approach uses nested loops to iterate through all possible pairs and track the minimum sum ? def min_pair(nums): min_sum = float('inf') min_pair = () ...

Read More

Python - Find Index Containing String in List

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 2K+ Views

When working with mixed-type lists in Python, you often need to find the indices containing strings. This is useful for text parsing, pattern matching, and extracting specific information from data structures. Here's an example scenario: my_list = [108, 'Safari', 'Skybags', 20, 60, 'Aristocrat', 78, 'Raj'] print("Original list:", my_list) Original list: [108, 'Safari', 'Skybags', 20, 60, 'Aristocrat', 78, 'Raj'] The goal is to find indices 1, 2, 5, 7 where strings are located in this mixed list. Using for loop with type() function The most straightforward approach uses a for loop ...

Read More

Find the Index of Maximum Item in a List using Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 15K+ Views

In Python, finding the index of the maximum item in a list is a common task. Python provides several built-in functions like max(), index(), and enumerate() that can be used to find the index of the maximum item in a list. For example, given the list [61, 12, 4, 5, 89, 7], the maximum value is 89 at index 4. Using index() with max() The simplest approach combines max() to find the maximum value and index() to find its position ? numbers = [61, 12, 4, 5, 89, 7] max_index = numbers.index(max(numbers)) print(f"Maximum value: {max(numbers)}") ...

Read More

How to change Tkinter Window Icon

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 17K+ Views

Tkinter is a popular GUI (Graphical User Interface) library in Python that provides a simple way to create desktop applications. You can customize the window icon using built−in functions like Tk(), PhotoImage(), and iconphoto(). Key Methods for Changing Window Icons Tk() Creates the main window of the tkinter application ? root = Tk() PhotoImage() A class that loads and displays images. The file parameter specifies the image location ? img = PhotoImage(file='image_path.png') iconphoto() Sets the window icon using a PhotoImage object. Takes two parameters: a boolean and the image variable ...

Read More

Python - Inter Matrix Grouping

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 216 Views

Inter Matrix Grouping is a technique where elements from two lists are grouped based on matching first elements. When matches are found, the first element becomes a dictionary key, and the remaining elements form grouped values. Python provides several built-in functions like setdefault(), defaultdict(), and append() to implement this efficiently. Understanding the Concept Let's understand this with an example: list1 = [[1, 2], [3, 4], [4, 5], [5, 6]] list2 = [[5, 60], [1, 22], [4, 59], [3, 14]] # Result: {1: [2, 22], 3: [4, 14], 4: [5, 59], 5: [6, 60]} ...

Read More

How to Iterate through a List without using the Increment Variable in Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 344 Views

Iteration allows you to process each element in a list without manually managing an increment variable. Python provides several built-in functions and techniques like enumerate(), map(), list comprehension, and direct iteration that eliminate the need for manual counter variables. Using Direct Iteration (Simple For Loop) The most straightforward way is to iterate directly over list elements ? fruits = ["apple", "banana", "orange", "grape"] for fruit in fruits: print(fruit) apple banana orange grape Using enumerate() Function When you need both index and value, enumerate() provides automatic ...

Read More

How to Initialize an Empty Array of given Length using Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 16K+ Views

An empty array consists of either null values or no elements. Empty arrays are useful when you need to initialize storage before populating it with data. Python provides several methods to create empty arrays of a given length using built-in functions and libraries. Using Multiplication (*) Operator The multiplication operator repeats elements to create an array of specified length ? length = 5 arr = [None] * length print("Empty array using multiplication operator:") print(arr) print("Length:", len(arr)) Empty array using multiplication operator: [None, None, None, None, None] Length: 5 Using NumPy empty() ...

Read More

Initialize a Dictionary with Custom Value list in Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 619 Views

Initializing a dictionary with custom value lists means creating a dictionary where each key maps to a list containing specific values. This approach is useful when you need to group multiple values under each key or when you want each value to be in list format for further manipulation. Syntax Overview Python provides several built-in functions to initialize dictionaries with custom value lists: range() - Returns a sequence of numbers len() - Returns the length of an object zip() - Combines multiple iterables dict() - Creates a dictionary enumerate() - Iterates with index tracking ...

Read More

Iterating through a range of dates in Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 8K+ Views

Iterating through a range of dates is a common task in Python applications. Python's datetime module provides several functions like date(), timedelta(), and built-in functions like range() to accomplish this efficiently. For example, if we want to iterate from 2023-06-26 to 2023-06-30, we would get: 2023-06-26 2023-06-27 2023-06-28 2023-06-29 2023-06-30 Key Functions datetime.date() − Creates date objects representing calendar dates. timedelta() − Represents a duration, the difference between two dates or times. ...

Read More
Showing 541–550 of 25,469 articles
« Prev 1 53 54 55 56 57 2547 Next »
Advertisements