 Open in App
Not now

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

• Last Updated : 22 Feb, 2023

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)

My Personal Notes arrow_drop_up