Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Summation of dictionary list values
Next article icon

Python – Index Value Summation List

Last Updated : 09 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

To access the elements of lists, there are various methods. But sometimes we may require to access the element along with the index on which it is found and compute its summation, and for that, we may need to employ different strategies. This article discusses some of those strategies.

Method 1: Naive method This is the most generic method that can be possibly employed to perform this task of accessing the index along with the value of the list elements. This is done using a loop. The task of performing sum is performed using external variable to add. 

Python3




# Python 3 code to demonstrate
# Index Value Summation List
# using naive method
 
# initializing list
test_list = [1, 4, 5, 6, 7]
 
# Printing list
print ("The original list is : " + str(test_list))
 
# using naive method to
# Index Value Summation List
res = []
for i in range(len(test_list)):
    res.append(i + test_list[i])
 
print ("The list index-value summation is : " + str(res))
 
 
Output : 
The original list is : [1, 4, 5, 6, 7] The list index-value summation is : [1, 5, 7, 9, 11]

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method 2: Using list comprehension + sum() This method works in similar way as the above method but uses the list comprehension technique for the same, this reduces the possible lines of code to be written and hence saves time. The task of performing summation is done by sum(). 

Python3




# Python 3 code to demonstrate
# Index Value Summation List
# using list comprehension
 
# initializing list
test_list = [1, 4, 5, 6, 7]
 
# Printing list
print ("The original list is : " + str(test_list))
 
# using list comprehension to
# Index Value Summation List
res = [sum(list((i, test_list[i]))) for i in range(len(test_list))]
 
print ("The list index-value summation is : " + str(res))
 
 
Output : 
The original list is : [1, 4, 5, 6, 7] The list index-value summation is : [1, 5, 7, 9, 11]

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method 3:  Using Enumerate
This method uses the built-in enumerate() method which is used to loop over a list along with the index of the elements present in it.

Python3




# Python 3 code to demonstrate
# Index Value Summation List
# using enumerate
   
# initializing list
test_list = [1, 4, 5, 6, 7]
   
# Printing list
print ("The original list is : " + str(test_list))
   
# using enumerate to
# Index Value Summation List
res = [index + value for index, value in enumerate(test_list)]
   
print ("The list index-value summation is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
 
 
Output
The original list is : [1, 4, 5, 6, 7] The list index-value summation is : [1, 5, 7, 9, 11]

Time complexity: O(n)
Auxiliary Space: O(n)

Method 4 : Using map() and lambda function:

  • The enumerate() function is used to generate a sequence of tuples (index, value) for each element in test_list.
  • The map() function is used to apply a lambda function to each tuple in the sequence.
  • The lambda function adds the index and value of each tuple together, which creates a new sequence of integers.
  • The list() function is used to convert the sequence of integers to a new list res.
  • The print() function is used to display the resulting list res to the user.

Python3




test_list = [1, 4, 5, 6, 7]
 
# Using map() and lambda function to sum the index
# and value for each element in test_list
 
# The enumerate() function is used to get the index
# and value of each element in the list
res = list(map(lambda x: x[0]+x[1], enumerate(test_list)))
 
print("The list index-value summation is : " + str(res))
 
 
Output
The list index-value summation is : [1, 5, 7, 9, 11]

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), as we create a new list res that contains the same number of elements as the input list. 



Next Article
Python | Summation of dictionary list values
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python | Summation of tuples in list
    Sometimes, while working with records, we can have a problem in which we need to find the cumulative sum of all the values that are present in tuples. This can have applications in cases in which we deal with a lot of record data. Let's discuss certain ways in which this problem can be solved. Metho
    7 min read
  • Python | Summation of dictionary list values
    Sometimes, while working with Python dictionaries, we can have its values as lists. In this can, we can have a problem in that we just require the count of elements in those lists as a whole. This can be a problem in Data Science in which we need to get total records in observations. Let's discuss c
    6 min read
  • Python - Index Value repetition in List
    Given a list of elements, The task is to write a Python program to repeat each index value as per the value in that index. Input : test_list = [3, 0, 4, 2] Output : [0, 0, 0, 2, 2, 2, 2, 3, 3] Explanation : 0 is repeated 3 times as its index value is 3. Input : test_list = [3, 4, 2] Output : [0, 0,
    7 min read
  • Python | i^k Summation in list
    Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding sum of i^k of list in just one line. Let’s discuss cer
    5 min read
  • Python - List of dictionaries all values Summation
    Given a list of dictionaries, extract all the values summation. Input : test_list = [{"Apple" : 2, "Mango" : 2, "Grapes" : 2}, {"Apple" : 2, "Mango" : 2, "Grapes" : 2}] Output : 12 Explanation : 2 + 2 +...(6-times) = 12, sum of all values. Input : test_list = [{"Apple" : 3, "Mango" : 2, "Grapes" : 2
    5 min read
  • Python - Key Lists Summations
    Sometimes, while working with Python Dictionaries, we can have problem in which we need to perform the replace of key with values with sum of all keys in values. This can have application in many domains that include data computations, such as Machine Learning. Let's discuss certain ways in which th
    9 min read
  • Python - Nested record values summation
    Sometimes, while working with records, we can have problems in which we need to perform summation of nested keys of a key and record the sum as key's value. This can have possible applications in domains such as Data Science and web development. Let us discuss certain ways in which this task can be
    7 min read
  • Python - Nested Dictionary values summation
    Sometimes, while working with Python dictionaries, we can have problem in which we have nested records and we need cumulative summation of it's keys values. This can have possible application in domains such as web development and competitive programming. Lets discuss certain ways in which this task
    8 min read
  • Python | Chuncked summation every K value
    The prefix array is quite famous in the programming world. This article would discuss a variation of this scheme. To perform a chunked summation where the sum resets at every occurrence of the value K , we need to modify the summation logic. The idea is: Traverse the list.Keep adding to the cumulati
    3 min read
  • Python | Accumulative index summation in tuple list
    Sometimes, while working with data, we can have a problem in which we need to find accumulative summation of each index in tuples. This problem can have applications in web development and competitive programming domain. Let's discuss certain way in which this problem can be solved. Method 1: Using
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences