Server Side Programming Articles

Page 552 of 2109

Alternate element summation in list (Python)

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 1K+ Views

The given task is to perform an alternate summation on a Python List. For example, adding the elements at even indices (0, 2, 4) or odd indices (1, 3, 5), depending on the requirement. This means we need to add every other element from the given list. Let us see an input scenario − Scenario Input: [11, 22, 33, 44, 55] Output: 99 Explanation: Here, we are going to add the elements at the even indices by using the slicing [::2]. Using sum() with List Slicing The alternate element summation in a list can ...

Read More

Python Grouped Flattening of list

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

In this tutorial, we will write a program that performs grouped flattening of a list containing sub-lists. This technique groups consecutive sublists together and flattens them into single lists, creating chunks of a specified size. Problem Statement Given a list of sublists and a number, we need to group the sublists in chunks of the given number and flatten each group into a single list. Input lists = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]] number = 2 Expected Output [[1, 2, 3, 4], [5, 6, 7, 8], ...

Read More

Python How to copy a nested list

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

In this tutorial, we are going to see different ways to copy a nested list in Python. A nested list is a list that contains other lists as elements. When copying nested lists, we need to create independent copies of both the outer list and inner lists to avoid unwanted modifications. Let's explore three effective methods to achieve this ? Using Nested Loops The most straightforward approach uses nested loops to manually copy each element ? # initializing a list nested_list = [[1, 2], [3, 4], [5, 6, 7]] # empty list copy_list = ...

Read More

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 337 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 415 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
Showing 5511–5520 of 21,090 articles
« Prev 1 550 551 552 553 554 2109 Next »
Advertisements