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 | Equal Keys List Summation
Next article icon

Python | i^k Summation in list

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

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 certain ways in which this can be performed. 

Method #1 : Using reduce() + lambda + pow() The power of lambda functions to perform lengthy tasks in just one line, allows it combined with reduce which is used to accumulate the subproblem, to perform this task as well. The pow() is used to perform task of computing power. Works with only Python 2. 

Python3




# Python code to demonstrate
# i ^ k Summation in list
# using reduce() + lambda + pow()
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# using reduce() + lambda + pow()
# i ^ k Summation in list
res = reduce(lambda i, j: i + pow(j, K), [pow(test_list[:1][0], K)] + test_list[1:])
 
# printing result
print ("The sum of i ^ k of list is : " + str(res))
 
 
Output : 
The original list is : [3, 5, 7, 9, 11] The sum of i^k of list is : 24309

Time complexity: O(n*n), where n is the length of the test_list. The reduce() + lambda + pow() takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required

  Method #2 : Using map() + sum() + pow() The similar solution can also be obtained using the map function to integrate and sum function to perform the summation of the i^k number. 

Python3




# Python3 code to demonstrate
# i ^ k Summation in list
# using map() + sum() + pow()
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# using map() + sum() + pow()
# i ^ k Summation in list
res = sum(map(lambda i : pow(i, K), test_list))
 
# printing result
print ("The sum of i ^ k of list is : " + str(res))
 
 
Output : 
The original list is : [3, 5, 7, 9, 11] The sum of i^k of list is : 24309

Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using map() + sum() + pow() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space 

Method #4 : Using Numpy

Note: Install numpy module using command “pip install numpy”

Python3




import numpy as np
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# Using Numpy
# i ^ k Summation in list
res = np.sum(np.power(test_list, K))
 
# printing result
print ("The sum of i ^ k of list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
 
 

Output:

The original list is : [3, 5, 7, 9, 11]
The sum of i ^ k of list is : 24309

This method uses the numpy module to perform the computation. The np.power() function is used to compute the power of each element in the input list, and the np.sum() function is used to add up the results.

Time Complexity: O(n)
Space Complexity: O(n)

Method #4 : Using a for loop:

Python3




#initializing list
test_list = [3, 5, 7, 9, 11]
# printing original list
print ("The original list is : " + str(test_list))
#initializing K
K = 4
res = 0
for i in test_list:
    res += pow(i, K)
# printing result
print("The sum of i ^ k of list is : " + str(res))
#This code is contributed by pinjala Jyothi.
 
 
Output
The original list is : [3, 5, 7, 9, 11] The sum of i ^ k of list is : 24309

Time Complexity: O(n)
Space Complexity: O(1)

Method#5: Using Recursive method.

Algorithm:

  1. Define a function sum_of_powers(test_list, K) that takes a list test_list and an integer K as inputs.
  2. If test_list is empty, return 0 as the base case.
  3. Otherwise, compute the sum of the first element of test_list raised to the power of K, and the sum of the rest of the
  4. elements of test_list computed recursively using the same function sum_of_powers with the tail of test_list.
  5. Return the sum computed in step 3 as the final result.

Python3




# Python program for the above approach
 
# Function to find the sum of powers
def sum_of_powers(test_list, K):
    if len(test_list) == 0:
        return 0
    else:
        return pow(test_list[0], K) + sum_of_powers(test_list[1:], K)
       
# Driver Code
test_list = [3, 5, 7, 9, 11]
K = 4
 
print ("The original list is : " + str(test_list))
 
res = sum_of_powers(test_list, K)
 
print("The sum of i ^ k of list is : " + str(res))
 
 
Output
The original list is : [3, 5, 7, 9, 11] The sum of i ^ k of list is : 24309

Time complexity:
The time complexity of this recursive implementation of the code is O(n), where n is the length of the input list test_list.

Space complexity:

The space complexity of the recursive implementation is O(n) as well, where n is the length of the input list test_list.



Next Article
Python | Equal Keys List Summation
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • 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 | Equal Keys List Summation
    Sometimes, while working with dictionaries, we can have a problem in which we have many dictionaries and we are required to sum like keys. This problem seems common, but complex is if the values of keys are list and we need to add elements to list of like keys. Let’s discuss way in which this proble
    4 min read
  • Python - Index Value Summation List
    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: Nai
    4 min read
  • 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 Grouping in Dictionary List
    Sometimes, while working with Python Dictionaries, we can have a problem in which we need to perform the grouping of dictionaries according to specific key, and perform summation of certain keys while grouping similar key's value. This is s peculiar problem but can have applications in domains such
    5 min read
  • Element indices Summation - Python
    Our task is to calculate the sum of elements at specific indices in a list. This means selecting elements at specific positions and computing their sum. Given a list and a set of indices, the goal is to compute the sum of elements present at those indices. For example, given the list [10, 20, 30, 40
    3 min read
  • Python | Alternate element summation in list
    The problem of getting summation of a list is quite generic and we might some day face the issue of getting the summation of alternate elements and get the list of 2 elements containing summation of alternate elements. Let's discuss certain ways in which this can be performed. Method #1 : Using list
    5 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 | 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
  • Python | Pair summation of list elements
    Sometimes, while working with Python list, one can have a problem in which one needs to find perform the summation of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let's discuss certain ways in which this problem can be solve
    4 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