Programming Articles

Page 556 of 2547

Python Indexing a sublist

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 2K+ Views

In this tutorial, we will learn how to find the index of a sublist that contains a specific element. This is useful when working with nested lists where you need to locate which sublist contains your target element. Problem Statement Given a nested list, we need to find the index of the sublist that contains a specific element ? nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Expected output for elements 7, 5, and 3 ? Index of 7: 2 Index of 5: 1 Index of 3: 0 ...

Read More

Python Index specific cyclic iteration in list

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 1K+ Views

In this tutorial, we will learn how to cyclically iterate through a list starting from a specific index. Cyclic iteration means when we reach the end of the list, we continue from the beginning until all elements are visited. Algorithm Steps Initialize the list and starting index Find the length of the list using len() Iterate over the list using the length Find the current element index using (start_index + i) % length Print the element at that index Example Let's implement cyclic iteration starting from index 5 ? # initializing the ...

Read More

Python Grouping similar substrings in list

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 3K+ Views

In this tutorial, we are going to write a program that groups similar substrings from a list. We'll use Python's itertools.groupby() method to group strings that share a common prefix. Problem Understanding Given a list of strings with a common pattern (prefix-suffix), we want to group them by their prefix. Input strings = ['tutorials-python', 'tutorials-c', 'tutorials-java', 'tutorials-javascript', 'python-1', 'python-2', 'javascript-1'] Expected Output [['tutorials-python', 'tutorials-c', 'tutorials-java', 'tutorials-javascript'], ['python-1', 'python-2'], ['javascript-1']] Solution Using itertools.groupby() The itertools.groupby() method groups consecutive elements that have the same key. We'll use a lambda function ...

Read More

Python Group elements at same indices in a multi-list

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 745 Views

In this tutorial, we will write a program that groups elements at the same indices from multiple lists into a single list. This operation is also known as transposing a matrix. All input lists must have the same length. Example Input and Output Let's see what we want to achieve − # Input [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Output [[1, 4, 7], [2, 5, 8], [3, 6, 9]] Elements at index 0 from all lists: [1, 4, 7], elements at index 1: [2, 5, 8], and ...

Read More

Python Group Anagrams from given list

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 4K+ Views

In this tutorial, we are going to write a program that groups all anagrams in a list. First, let's see what are anagrams. Any two strings that have the same characters in a different order are known as anagrams. Before diving into the solution, let's see an example. Example Input and Output words = ['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac'] print("Input:", words) Input: ['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac'] Expected output: [['cat', 'tac'], ['dog', 'god'], ['fired', 'fried'], ['pat', 'tap']] Understanding the Approach ...

Read More

Python Getting sublist element till N

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 336 Views

In this tutorial, we will learn how to extract the first element from each sublist up to the Nth sublist in a nested list. For example, if we have a list with 5 sublists and want the first element from the first 3 sublists, Python provides several approaches to accomplish this. Let's explore different methods using the following example list: programming_languages = [['Python', 'Java'], ['C', 'Pascal'], ['Javascript', 'PHP'], ['C#', 'C++'], ['React', 'Angular']] print("Original list:", programming_languages) Original list: [['Python', 'Java'], ['C', 'Pascal'], ['Javascript', 'PHP'], ['C#', 'C++'], ['React', 'Angular']] Using For Loop The ...

Read More

Program to check congruency of two triangles in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 413 Views

In geometry, two triangles are congruent if they have the same shape and size. We can check triangle congruency using three main criteria: SSS (Side-Side-Side), SAS (Side-Angle-Side), and ASA (Angle-Side-Angle). Note that AAA (Angle-Angle-Angle) only proves similarity, not congruency. Let's implement functions to check each congruency condition ? SSS (Side-Side-Side) Congruency Two triangles are congruent if all three corresponding sides are equal ? def side_side_side_congruent(sides_one, sides_two): # Sort both lists to compare corresponding sides sides_one_sorted = sorted(sides_one) sides_two_sorted = sorted(sides_two) ...

Read More

Program for longest common directory path in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 536 Views

In this tutorial, we are going to write a program that finds the longest common directory path from a given list of paths. This is useful when working with file systems or organizing directory structures. Problem Statement Given a list of file paths, we need to find the longest directory path that is common to all of them ? paths = ['home/tutorialspoint/python', 'home/tutorialspoint/c', 'home/tutorialspoint/javascript', 'home/tutorialspoint/react', 'home/tutorialspoint/django'] Expected Output: home/tutorialspoint Using os.path.commonprefix() Python's os module provides os.path.commonprefix() to find the common prefix of multiple ...

Read More

Binary Prefix Divisible By 5 in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 224 Views

Given an array A of 0s and 1s, we need to determine which binary prefixes are divisible by 5. For each index i, we interpret the subarray from A[0] to A[i] as a binary number and check if it's divisible by 5. For example, if the input is [0, 1, 1, 1, 1, 1], the binary prefixes are: 0 (binary) = 0 (decimal) → divisible by 5 01 (binary) = 1 (decimal) → not divisible by 5 011 (binary) = 3 (decimal) → not divisible by 5 0111 (binary) = 7 (decimal) → not divisible by 5 ...

Read More

Powerful Integers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 309 Views

A powerful integer is defined as a number that can be expressed in the form xi + yj where x and y are positive integers, and i, j are non-negative integers. Given two positive integers x and y along with a bound, we need to find all powerful integers less than or equal to the bound. Understanding Powerful Integers For example, with x = 2, y = 3, and bound = 10: 2 = 20 + 30 = 1 + 1 3 = 21 + 30 = 2 + 1 4 = 20 + 31 = ...

Read More
Showing 5551–5560 of 25,466 articles
« Prev 1 554 555 556 557 558 2547 Next »
Advertisements