
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Prefix sum list
A list is a collection which is ordered and changeable. In Python lists are written with square brackets. You access the list items by referring to the index number. Negative indexing means beginning from the end, -1 refers to the last item. You can specify a range of indexes by specifying where to start and where to end the range. When specifying a range, the return value will be a new list with the specified items.
Example
# using list comprehension + sum() + list slicing # initializing list test_list = [3, 4, 1, 7, 9, 1] # printing original list print("The original list : " + str(test_list)) # using list comprehension + sum() + list slicing # prefix sum list res = [sum(test_list[ : i + 1]) for i in range(len(test_list))] # print result print("The prefix sum list is : " + str(res))
Output
The original list : [3, 4, 1, 7, 9, 1] The prefix sum list is : [3, 7, 8, 15, 24, 25]
- Related Articles
- Python – Substitute prefix part of List
- Prefix sum array in python using accumulate function
- Python Program to print strings based on the list of prefix
- Program to find longest common prefix from list of strings in Python
- K Prefix in Python
- Maximum subarray sum in O(n) using prefix sum in C++
- Longest Common Prefix in Python
- Prefix Sum of Matrix (Or 2D Array) in C++
- Maximum prefix-sum for a given range in C++
- Maximum sum increasing subsequence from a prefix and a given element after prefix is must in C++
- Nested List Weight Sum II in Python
- Python | Sum of number digits in List
- Implement Trie (Prefix Tree) in Python
- Python – Split Strings on Prefix Occurrence
- Sum of list (with string types) in Python

Advertisements