- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to find the group sum till each K in a list
When it is required to find the group sum till each K in a list, a simple iteration and the ‘append’ method are used.
Below is a demonstration of the same
from collections import defaultdict my_list = [21, 4, 37, 46, 7, 56, 7, 69, 2, 86, 1] print("The list is :") print(my_list) my_key = 46 print("The key is ") print(my_key) my_sum = 0 my_result =  for ele in my_list: if ele != my_key: my_sum += ele else: my_result.append(my_sum) my_result.append(ele) my_sum = 0 my_result.append(my_sum) print("The resultant list is :") print(my_result)
The list is : [21, 4, 37, 46, 7, 56, 7, 69, 2, 86, 1] The key is 46 The resultant list is : [62, 46, 228]
The required packages are imported into the environment.
A list is defined and is displayed on the console.
A key is defined and displayed on the console.
The sum value is assigned to 0.
An empty list is defined.
The list is iterated over, and if the element in the list is not equal to the key value, it is added to the sum.
Otherwise, the sum and the specific is appended to the empty list.
The sum is reinitialized to 0.
This sum is finally appended to the empty list.
This is displayed as output on the console.
- Program to find sum of the minimums of each sublist from a list in Python
- Program to find number of sublists with sum k in a binary list in Python
- Program to Find K-Largest Sum Pairs in Python
- Program to find the sum of largest K sublist in Python
- Python Program to find the cube of each list element
- Program to find a list of numbers where each K-sized window has unique elements in Python
- Python program to find Cumulative sum of a list
- Program to find maximum sum of popped k elements from a list of stacks in Python
- Program to find sum of concatenated pairs of all each element in a list in Python?\n
- Program to Find Out the Largest K-Divisible Subsequence Sum in Python
- Adding K to each element in a Python list of integers
- Program to find three unique elements from list whose sum is closest to k Python
- Java Program to Find Even Sum of Fibonacci Series till number N
- Program to find sum of rectangle whose sum at most k in Python
- Program to find the K-th last node of a linked list in Python