Programming Articles

Page 896 of 2547

Get first element of each sublist in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 5K+ Views

A list in Python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the first element of each sublist in a given list. Using for Loop It is a very simple approach in which we loop through the sublists fetching the item at index 0 in them. A for loop is used for this purpose as shown below ? Example nested_list = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]] print("Given List:", nested_list) print("First Items from sublists:") for item ...

Read More

Count unique sublists within list in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 619 Views

A Python list can contain sublists, which are lists nested within a larger list. In this article we will explore how to count the number of unique sublists within a given list using two different approaches. Using Counter Counter is a subclass of Dictionary used to keep track of elements and their count. It stores elements as dictionary keys and their count as dictionary values. To count sublists, we convert each sublist to a string representation ? Example from collections import Counter # Given List with sublists days_list = [['Mon'], ['Tue', 'Wed'], ['Tue', 'Wed']] ...

Read More

Assign ids to each unique value in a Python list

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 1K+ Views

When working with Python lists, you may need to assign unique IDs to each distinct value while ensuring that duplicate values receive the same ID. This is useful for data analysis, categorization, and indexing operations. Using enumerate() and OrderedDict.fromkeys() The enumerate() function creates a counter for each element, while OrderedDict.fromkeys() preserves the first occurrence order and eliminates duplicates ? from collections import OrderedDict values = ['Mon', 'Tue', 'Wed', 'Mon', 5, 3, 3] print("The given list:", values) # Assigning ids to values list_ids = [{v: k for k, v in enumerate( ...

Read More

Python to Find number of lists in a tuple

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 417 Views

A Python tuple is ordered and unchangeable. It can contain lists as its elements. Given a tuple made up of lists, let's find out how many lists are present in the tuple using different approaches. Using len() Function The simplest approach is to use the built-in len() function, which returns the count of elements (lists) in the tuple ? tupA = (['a', 'b', 'x'], [21, 19]) tupB = (['n', 'm'], ['z', 'y', 'x'], [3, 7, 89]) print("The number of lists in tupA:", len(tupA)) print("The number of lists in tupB:", len(tupB)) The output of ...

Read More

Find missing numbers in a sorted list range in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 1K+ Views

Given a sorted list of numbers, we want to find out which numbers are missing from the continuous range. Python provides several approaches to identify these gaps in the sequence. Using range() with List Comprehension We can create a complete range from the first to last element and check which numbers are not in the original list ? numbers = [1, 5, 6, 7, 11, 14] # Original list print("Given list:", numbers) # Find missing numbers using range missing = [x for x in range(numbers[0], numbers[-1] + 1) ...

Read More

Find missing elements in List in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 2K+ Views

When working with a list of numbers, you may need to find which values are missing from a contiguous sequence. Python provides several approaches to identify missing elements from 0 to the maximum value in the list. Using List Comprehension with range() and max() The most straightforward approach uses list comprehension to check each number in the range and identify which ones are not in the original list ? numbers = [1, 5, 6, 7, 11, 14] # Original list print("Given list:", numbers) # Find missing elements using list comprehension missing = [num for ...

Read More

Find mismatch item on same index in two list in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 3K+ Views

Sometimes we need to compare elements in two Python lists based on both their value and position. This article shows different methods to find indices where elements at the same position have different values. Using for Loop We can design a for loop to compare values at similar indexes. If the values do not match, we add the index to a result list ? listA = [13, 'Mon', 23, 62, 'Sun'] listB = [5, 'Mon', 23, 6, 'Sun'] # Index variable idx = 0 # Result list res = [] # With iteration ...

Read More

Find Min-Max in heterogeneous list in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 685 Views

A Python list can contain both strings and numbers, creating what we call a heterogeneous list. In this article, we will explore different methods to find the minimum and maximum numeric values from such mixed-type lists. Finding Minimum Value We can use the isinstance() function to filter only numeric values and then apply the min() function to find the smallest number. Example data = [12, 'Sun', 39, 5, 'Wed', 'Thu'] # Given list print("The Given list:", data) # Filter integers and find minimum res = min(i for i in data if isinstance(i, int)) ...

Read More

Find Maximum difference pair in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 592 Views

Data analysis often requires finding pairs of elements with specific characteristics. In this article, we will explore how to find pairs of numbers in a list that have the maximum difference between them using different Python approaches. Using nlargest() with combinations() This approach finds all possible combinations of elements, calculates their differences, and uses nlargest() from the heapq module to get multiple pairs with maximum differences. Example from itertools import combinations from heapq import nlargest numbers = [21, 14, 30, 11, 17, 18] print("Given list:", numbers) # Find top 2 pairs with ...

Read More

Find longest consecutive letter and digit substring in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 472 Views

A given string may be a mixture of digits and letters. In this article, we will find the longest consecutive substring of letters and the longest consecutive substring of digits separately using two different approaches. Using Regular Expression Module The regular expression module can identify all continuous substrings containing only digits or only letters. We use findall() to extract these substrings and max() with key=len to find the longest ones ? Example import re def longSubstring(text): # Find all consecutive letter sequences letter = max(re.findall(r'\D+', text), ...

Read More
Showing 8951–8960 of 25,466 articles
« Prev 1 894 895 896 897 898 2547 Next »
Advertisements