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 program to extract characters in given range from a string list
Next article icon

Python program to find the sum of Characters ascii values in String List

Last Updated : 08 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given the string list, the task is to write a Python program to compute the summation value of each character’s ASCII value.

Examples:

Input : test_list = [“geeksforgeeks”, “teaches”, “discipline”] 
Output : [133, 61, 100] 
Explanation : Positional character summed to get required values.

Input : test_list = [“geeksforgeeks”, “discipline”] 
Output : [133, 100] 
Explanation : Positional character summed to get required values. 

Method 1 : Using ord() + loop

In this, we iterate each character in each string and keep on adding positional values to get its sum. The summed value is appended back to the result in a list.

Python3




# Python3 code to demonstrate working of
# Characters Positions Summation in String List
# Using ord() + loop
 
# initializing list
test_list = ["geeksforgeeks",
             "teaches", "us", "discipline"]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = []
for sub in test_list:
    ascii_sum = 0
     
    # getting ascii value sum
    for ele in sub :
        ascii_sum += (ord(ele) - 96)
         
    res.append(ascii_sum)
 
# printing result
print("Position Summation List : " + str(res))
 
 

Output:

The original list is : [‘geeksforgeeks’, ‘teaches’, ‘us’, ‘discipline’] Position Summation List : [133, 61, 40, 100]

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

Method 2 : Using list comprehension + sum() + ord()

In this, we get summation using sum(), ord() is used to get the ASCII positional value, and list comprehension offers one-liner solution to this problem.

Python3




# Python3 code to demonstrate working of
# Characters Positional Summation in String List
# Using list comprehension + sum() + ord()
 
# initializing list
test_list = ["geeksforgeeks", "teaches",
             "us", "discipline"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# sum() gets summation, list comprehension
# used to perform task in one line
res = [sum([ord(ele) - 96 for ele in sub]) for sub in test_list]
 
# printing result
print("Positional Summation List : " + str(res))
 
 

 Output:

The original list is : [‘geeksforgeeks’, ‘teaches’, ‘us’, ‘discipline’] Position Summation List : [133, 61, 40, 100]

The time and space complexity for all the methods are the same:

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

Method 3 : Using map and lambda

Here’s another approach using map() function to calculate the summation of ASCII values of each character:

Python3




# Python3 code to demonstrate working of
# Characters Positional Summation in String List
# Using map() function
  
# initializing list
test_list = ["geeksforgeeks", "teaches",
             "us", "discipline"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# map() function to calculate sum of ASCII values
res = list(map(lambda x: sum(map(lambda y: ord(y) - 96, x)), test_list))
  
# printing result
print("Positional Summation List : " + str(res))
 
 
Output
The original list is : ['geeksforgeeks', 'teaches', 'us', 'discipline'] Positional Summation List : [133, 61, 40, 100] 

This method also has the same time and space complexity as the other two methods, i.e.,
Time Complexity: O(n^2)
Auxiliary Space: O(n)

Method 4 : using the reduce function from the functools module along with a lambda function.

Step-by-step approach:

  • Import the functools module.
  • Initialize the list containing the strings.
  • Define a lambda function that takes two arguments a and b, and returns the sum of their ordinal values (ASCII value – 96).
  • Use the reduce function along with the lambda function to get the positional summation of the characters in each string.
  • Print the resultant list.

Python3




import functools
 
# initializing list
test_list = ["geeksforgeeks", "teaches", "us", "discipline"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using reduce() + lambda to compute positional summation
res = [functools.reduce(lambda a, b: a + ord(b) - 96, sub, 0) for sub in test_list]
 
# printing result
print("Positional Summation List : " + str(res))
 
 
Output
The original list is : ['geeksforgeeks', 'teaches', 'us', 'discipline'] Positional Summation List : [133, 61, 40, 100] 

Time complexity: O(n*m), where n is the number of strings in the list and m is the maximum length of any string.
Auxiliary space: O(n), as we are storing the positional summation of each string in a list.



Next Article
Python program to extract characters in given range from a string list
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • ASCII
  • Python list-programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • Python Program to Find ASCII Value of a Character
    Given a character, we need to find the ASCII value of that character using Python. ASCII (American Standard Code for Information Interchange) is a character encoding standard that employs numeric codes to denote text characters. Every character has its own ASCII value assigned from 0-127. Examples:
    2 min read
  • Python program to calculate the number of words and characters in the string
    We are given a string we need to find the total number of words and total number of character in the given string. For Example we are given a string s = "Geeksforgeeks is best Computer Science Portal" we need to count the total words in the given string and the total characters in the given string.
    3 min read
  • Python Program to Find the Total Sum of a Nested List Using Recursion
    A nested list is given. The task is to print the sum of this list using recursion. A nested list is a list whose elements can also be a list. Examples : Input: [1,2,[3]] Output: 6 Input: [[4,5],[7,8,[20]],100] Output: 144 Input: [[1,2,3],[4,[5,6]],7] Output: 28 Recursion: In recursion, a function ca
    5 min read
  • Ways to Convert List of ASCII Value to String - Python
    The task of converting a list of ASCII values to a string in Python involves transforming each integer in the list, which represents an ASCII code, into its corresponding character. For example, with the list a = [71, 101, 101, 107, 115], the goal is to convert each value into a character, resulting
    3 min read
  • Python program to extract characters in given range from a string list
    Given a Strings List, extract characters in index range spanning entire Strings list. Input : test_list = ["geeksforgeeks", "is", "best", "for", "geeks"], strt, end = 14, 20 Output : sbest Explanation : Once concatenated, 14 - 20 range is extracted.Input : test_list = ["geeksforgeeks", "is", "best",
    4 min read
  • Python program to find occurrence to each character in given string
    Given a string, the task is to write a program in Python that prints the number of occurrences of each character in a string. There are multiple ways in Python, we can do this task. Let's discuss a few of them. Method #1: Using set() + count() Iterate over the set converted string and get the count
    5 min read
  • Python program to find sum of elements in list
    Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently. Using sum()sum() function is t
    3 min read
  • Python program to print words from a sentence with highest and lowest ASCII value of characters
    Given a string S of length N, representing a sentence, the task is to print the words with the highest and lowest average of ASCII values of characters. Examples: Input: S = "every moment is fresh beginning"Output:Word with minimum average ASCII values is "beginning".Word with maximum average ASCII
    5 min read
  • Python program to convert a byte string to a list of integers
    We have to convert a byte string to a list of integers extracts the byte values (ASCII codes) from the byte string and stores them as integers in a list. For Example, we are having a byte string s=b"Hello" we need to write a program to convert this string to list of integers so the output should be
    2 min read
  • Convert String List to ASCII Values - Python
    We need to convert each character into its corresponding ASCII value. For example, consider the list ["Hi", "Bye"]. We want to convert it into [[72, 105], [66, 121, 101]], where each character is replaced by its ASCII value. Let's discuss multiple ways to achieve this. Using List Comprehension with
    3 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