0

Code:

>>> sum(['1','2'])

Error:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

As I haven't passed integer in the list, I didn't expect it is summing int and str. Both are strings.

Can anyone please explain, why error say `int?

Python coder
  • 743
  • 5
  • 18
  • You need to give `sum` a list of integers, not a list of strings. Your computer thinks, rightfully so, that `'1'` and `1` are different. – awakenedhaki Sep 03 '19 at 06:40
  • @awakenedhaki Correct, but the question is different. – Selcuk Sep 03 '19 at 06:42
  • And if you do want to concatinate the strings '1' and '2' and get '12', just do `'1' + '2'` – Aryerez Sep 03 '19 at 06:42
  • @awakenedhaki `sum` basically just applies `+` between all values, which works as well for strings… – deceze Sep 03 '19 at 06:43
  • I know that, `sum` needs a iterable item which consists `integers` to work properly. My question is, as I have passed two strings, it should not do `1+'2'`. Is it explicitly converting to integer? I want to understand, what actually happening, that it is processed as `int + str`. – Python coder Sep 03 '19 at 06:45
  • You did notice that your question is closed as a duplicate of something that will answer this…? – deceze Sep 03 '19 at 06:46
  • @deceze While it is true that `str` also uses `+`, the way that `sum` is implemented does not allow you to pass a list of strings to it. If you go on and try it, you will get an error. If you would use `+` to build a string using a `sum` you would have to incrementally create `n` of temporarily immutable strings. This is not efficient, and therefore that is why you are encouraged to use `''.join(list_of_strings)` – awakenedhaki Sep 03 '19 at 06:46
  • @deceze Why it is marked as duplicate? This is completely a different question. I want to know the reason for error. In error it is saying `int`, where I haven't given `int` in list. – Python coder Sep 03 '19 at 06:49
  • @user2357112 My question is regarding the understanding of error not about implementation. This is probably not the duplicate of mentioned dupe link – Python coder Sep 03 '19 at 06:51
  • Added a [better duplicate](https://stackoverflow.com/a/1218735/476) now. – deceze Sep 03 '19 at 06:57
  • 1
    @Pythoncoder see you are given two string in list and that list you will give into sum(), sum() work is giving sum of all elements of the given list right. sum() only work when there are all integers okay. so, first of all, it will start from 0 + your first element of the list, as per your list first element of a list is '1' so, it will try as 0 + '1' but there were '1' is string thus it will give TypeError: unsupported operand type(s) for +: 'int' and 'str' – MK Patel Sep 03 '19 at 07:01
  • @deceze Please look at MK Patel's comment. This is the right answer, I think. – Python coder Sep 03 '19 at 07:05
  • 1
    @MKPatel Thanks for explanation. Cuz of initial `0` which is integer, we are getting that error. – Python coder Sep 03 '19 at 07:09
  • @Pythoncoder Yes Exactly. Happy Codding :) – MK Patel Sep 03 '19 at 07:10

0 Answers0