How to sum a list of strings in python

Use type() and isdigit() functions in Python to achieve a sum list of strings in Python. This function will check If the element is int, then add it to the total by checking two conditions.

Simple example code adds all numbers from the list. Where given list may contain numbers in string or integer format.

lst = [1, '10', 'Hello', '2020', '[email protected]', 2021] total = 0 # iterating over the list for element in lst: # checking whether its a number or not if isinstance(element, int) or element.isdigit(): # adding the element to the total total += int(element) print(total)

Output:

Or use List comprehension

total = sum([int(i) for i in lst if type(i)== int or i.isdigit()])

How to sum a list of string objects in Python?

Answer: Prints only the first letter of each word in order to make one new word.

if words are separated by space, then split in words using space and for each word (“map” function), take the first character (x[0]). Last join the result using void.

s = "this is my sentence" res = "".join(map(lambda x: x[0], s.split(" "))) print(res)

Or simple use

res = "".join(x[0] for x in s.split())

Output: tims

Do comment if you have any doubts or suggestions on this Python sum topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

I want to make a function that takes a list which contains some words and prints only the first letter of each word in order to make one new word.

So my function is right here:

def word(x): li = [] for w in x: li.append(w[0]) return sum(li)

But unfortunately, it gives me the following error:
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Can you please explain me why is this happening ?

asked Dec 21, 2017 at 13:32

Kostas PetrakisKostas Petrakis

833 gold badges3 silver badges8 bronze badges

4

assuming words are separated by space,

S="this is my sentense" "".join(map(lambda x: x[0], S.split(" ")))

returns

'tims'

explanation :

  • split in words using space
  • for each word ("map" function), take first character (x[0])
  • then join the result using void

answered Dec 21, 2017 at 13:37

6

In this tutorial, we are going to write a program that adds all numbers from the list. List may contain numbers in string or integer format. See the example.

Input

random_list = [1, '10', 'tutorialspoint', '2020', 'tutorialspoint@2020', 2020]

Output

4051

Follow the below steps to write the program.

  • Initialize the list.
  • 3Initialize a variable total with 0.
  • Iterate over the list.
  • If the element is int, then add it to the total by checking two conditions.
    • The element will be int -> Check type.
    • The element will be a number in string format -> Check using isdigit() method.
  • Print the total

Example

 Live Demo

# initialzing the list random_list = [1, '10', 'tutorialspoint', '2020', 'tutorialspoint@2020', 2020] # initializing the variable total total = 0 # iterating over the list for element in random_list:    # checking whether its a number or not    if isinstance(element, int) or element.isdigit():       # adding the element to the total       total += int(element) # printing the total print(total)

Output

If you run the above code, then you will get the following result.

4051

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

Updated on 11-Jul-2020 08:00:30

  • Related Questions & Answers
  • Combinations of sum with tuples in tuple list in Python
  • Convert list of string to list of list in Python
  • Python - Convert list of string to list of list
  • Python | Sum of number digits in List
  • Python - Prefix sum list
  • Sum of list with stream filter in Java
  • Types of Linked List in Javascript
  • Convert list of numerical string to list of Integers in Python
  • Convert list of string into sorted list of integer in Python
  • Find sum of elements in list in Python program
  • Convert a string representation of list into list in Python
  • Python program to find sum of elements in list
  • Find all triplets in a list with given sum in Python
  • Java String Concatenation with Other Data Types.
  • How to convert string representation of list to list in Python?

How do you sum up a list in Python?

Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.

How do you sum a string?

Algorithm:.
Take a String..
Convert it into array of characters..
Apply for loop till length of char array..
Using isDigit() method we can check the digits in string..
If isDigit() will return true then print that index value..
That digit is in char form. ... .
Using sum variable, we will sum it..

How do you sum a float list in Python?

To get the sum of a list of float numbers that are wrapped in strings:.
Use the map() function to convert each string to a float..
Pass the result to the math. fsum() method..
The math. fsum() method will return the sum of the float numbers in the iterable..

Chủ đề