Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Tapas Kumar Ghosh
Page 3 of 19
Python - Indices of Atmost K Elements in List
Finding indices of elements that are at most K means locating positions of elements that are less than or equal to a given value K. Python provides several approaches using built-in functions like enumerate(), filter(), and NumPy functions. Problem Statement Given a list [10, 8, 13, 29, 7, 40, 91] and K value of 13, we need to find indices of elements ≤ 13. The elements [10, 8, 13, 7] are at positions [0, 1, 2, 4]. Using List Comprehension List comprehension with enumerate() provides a clean solution − k = 10 numbers = [10, 4, 11, 12, 14] indices = [i for i, num in enumerate(numbers) if num
Read MorePython - Frequency of Elements from Other List
Finding the frequency of elements from one list based on another list is a common data analysis task in Python. This involves counting how many times each element from a reference list appears in a data list. For example, given these two lists: reference_list = [1, 2, 3, 4] data_list = [1, 1, 2, 1, 2, 2, 2, 3, 4, 4, 4, 4, 4] # Result: {1: 3, 2: 4, 3: 1, 4: 5} Using Counter() The Counter class from the collections module efficiently counts occurrences of all elements ? from collections ...
Read MorePython - K difference Consecutive Element
The K difference consecutive element problem involves checking if consecutive elements in a list have a specific difference value K. This is useful in data analysis and algorithm problems where you need to validate sequential patterns. Let's understand with an example ? Given list: [5, 6, 3, 2, 4, 3, 4] and K = 1 We check each consecutive pair: |5-6|=1 (True), |6-3|=3 (False), |3-2|=1 (True), etc. Result: [True, False, True, False, True, True] Method 1: Using Brute Force Approach The simplest approach iterates through the list and compares each consecutive pair using the absolute difference ...
Read MoreAll possible concatenations in String List Using Python
All possible concatenations in a string list means to generate every possible combination of strings by concatenating two or more elements from a given list of strings. In this article, we will explore different approaches to find all possible concatenations from a list of strings. Following are the input-output scenarios for concatenating string lists − Example Scenarios Scenario 1: Two Strings Input: strings = ['TP', 'Tutorix'] Output: ['TP', 'Tutorix', 'TPTutorix', 'TutorixTP'] Explanation: When the list contains two strings 'TP' and 'Tutorix', it returns the original strings plus two concatenated combinations: 'TPTutorix' and 'TutorixTP'. ...
Read MoreAge Calculator using Python Tkinter
An age calculator in Python using Tkinter is a GUI (Graphical User Interface) application that allows users to determine their age based on their date of birth (DOB). To design the user interface of this application, we use various methods provided by the Tkinter library such as Tk(), pack(), Entry(), Button(), and more. GUI of Age Calculator Let us build a GUI-based age calculator where the user can enter their date of birth in the format of YYYY-MM-DD. The application will calculate and display the age in years, months, and days. ...
Read MoreFilter key from Nested item using Python
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 MorePython - Find Minimum Pair Sum in list
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 MorePython - Find Index Containing String in List
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 MoreFind the Index of Maximum Item in a List using Python
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 MoreHow to change Tkinter Window Icon
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