Tapas Kumar Ghosh

Tapas Kumar Ghosh

185 Articles Published

Articles by Tapas Kumar Ghosh

Page 2 of 19

Finding Words with both Alphabet and Numbers using Python

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

Finding words that contain both alphabetic characters and numeric digits is a common text processing task in Python. This tutorial demonstrates four different approaches using built-in functions like filter(), isdigit(), isalpha(), and regular expressions. Problem Overview Given a text string, we need to extract words that contain both letters and numbers ? Input my_text = "WELCOME TO CLUB100" print("Input:", my_text) Input: WELCOME TO CLUB100 Expected Output ['CLUB100'] Method 1: Using Regular Expressions Regular expressions provide a powerful pattern-matching approach to find words containing both letters and digits ...

Read More

Python - Find first Element by Second in Tuple List

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

Finding the first element by the second in a tuple list is a common operation in Python. Given a list of tuples where each tuple contains two elements, you need to search for a specific second element and return its corresponding first element. This is useful for key-value lookups and data filtering operations. Let's understand this with an example ? # Example tuple list countries = [("India", 35), ("Indonesia", 12), ("London", 31), ("Germany", 20)] # Search for the country with population 31 search_value = 31 # Expected output: "London" Method 1: Using a for ...

Read More

Convert Matrix to Coordinate Dictionary in Python

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

A coordinate dictionary is a dictionary where keys are tuples representing (row, column) positions and values are the non-zero elements from a matrix. This conversion is useful for sparse matrix representation where most elements are zero. Syntax The following built-in functions are commonly used for matrix to coordinate dictionary conversion − len(object) # Returns length of an object range(start, stop) # Returns sequence of numbers enumerate(iterable) # Returns index and value pairs zip(*iterables) # Combines ...

Read More

Python- Find the indices for k Smallest Elements

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

Finding indices of k smallest elements is a common task in data analysis and algorithm problems. Python provides several efficient approaches using built-in functions like sorted(), heapq, and NumPy's argsort(). Using sorted() with Lambda Function This approach sorts indices based on their corresponding values ? def k_smallest_indices(numbers, k): sorted_indices = sorted(range(len(numbers)), key=lambda i: numbers[i]) return sorted_indices[:k] # Create the list numbers = [50, 20, 90, 10, 70] k = 3 small_indices = k_smallest_indices(numbers, k) print("The k smallest indices are:", small_indices) print("Values at those indices:", [numbers[i] for ...

Read More

Python - Find dictionary keys Present in a Strings List

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

A dictionary is one of Python's core data types consisting of key-value pairs. A strings list contains elements represented as strings. Python provides several built-in functions like keys(), set(), intersection(), and append() to find dictionary keys that are present in a strings list. Let's take an example: Given dictionary: {'T': 1, 'M': 2, 'E': 3, 'C': 4} Given list: ['T', 'A', 'B', 'P', 'E', 'L'] Result: ['T', 'E'] Key Functions Used keys() − Returns all keys from a dictionary as a view object. set() − Creates a set object to store unique elements and ...

Read More

Finding the frequency of a given Datatype in a Python tuple

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

A tuple is a popular data structure in Python that stores multiple elements separated by commas. Finding the frequency of a specific data type means counting how many elements in the tuple belong to that type. Python provides several built-in functions like type(), isinstance(), filter(), and lambda to accomplish this task. Key Functions Before exploring the methods, let's understand the core functions used ? type() − Returns the exact type of an object isinstance() − Checks if an object is an instance of a specific type filter() − Filters elements based on a condition lambda − ...

Read More

Check Duplicate in a Stream of Strings

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

A stream of strings is a sequential flow of string data where each element represents an individual string. In Python, we can efficiently check for duplicates in a stream using data structures like sets or dictionaries to track previously seen strings. Using a Set to Track Duplicates The most efficient approach uses a set to store unique strings we've already encountered. Sets provide O(1) average-case lookup time, making duplicate detection fast ? Example def check_duplicate_in_stream(strings): seen = set() results = [] ...

Read More

Python - Find Keys with Specific Suffix in Dictionary

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

Finding keys with a specific suffix in a dictionary is a common task when filtering data based on key patterns. Python provides several approaches to accomplish this using built-in functions like endswith(), filter(), and comprehensions. Let's consider this dictionary example − my_dict = {'compiler': 100, 'interpreter': 200, 'cooperative': 300, 'cloudbox': 400} suffix = 'er' print("Original dictionary:", my_dict) print("Looking for keys ending with:", suffix) Original dictionary: {'compiler': 100, 'interpreter': 200, 'cooperative': 300, 'cloudbox': 400} Looking for keys ending with: er Key Methods Used endswith() − Returns True if the string ends with ...

Read More

Python - Interconvert Tuple to Byte Integer

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

Converting tuples to byte integers is useful in data serialization and low-level programming. Python provides several methods including int.from_bytes(), the struct module, and bitwise operations. What is Tuple to Byte Integer Conversion? A tuple is an ordered collection of elements represented using parentheses (). A byte integer is a whole number that represents bytes in memory. Converting tuples to byte integers involves treating tuple elements as byte values and combining them into a single integer. Method 1: Using int.from_bytes() The int.from_bytes() method converts a bytes object to an integer. Combined with bytes(), it provides the most ...

Read More

Python - Index Match Element Product

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

The Index Match Element Product refers to finding elements at the same positions in two lists that have equal values, then calculating their product. For example, if two lists have matching elements at positions 0 and 2, we multiply those matched elements together. Let's understand this with an example ? list_1 = [10, 20, 30, 40] list_2 = [10, 29, 30, 10] print("List 1:", list_1) print("List 2:", list_2) print("Matches at index 0: 10 == 10") print("Matches at index 2: 30 == 30") print("Product: 10 * 30 = 300") List 1: [10, 20, 30, ...

Read More
Showing 11–20 of 185 articles
« Prev 1 2 3 4 5 19 Next »
Advertisements