# can i join lists with sum()?

• Last Update :
• Techknowledgy :

If you check `sum()`'s documentation, you will see that, when you do not pass your empty list, the default value of that parameter (called `start`) is zero:

` sum(iterable, /, start=0)`

Then, `sum()` will get each value from the list you gave it and try to add up to its `start` parameter at first. It means it will try to add up zero to the first element of your list, which is also a list. And what happens when you try to add a list to a number? A `TypeError`:

```>>> 0 + [1, 2, 3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'list'```

When you pass the empty list, then the first thing `sum()` does is to add up that empty list to your first list. In this case, there should have no error (although, as expected, the result will not be different from the first element):

```>>> [] + [1, 2, 3]
[1, 2, 3]```

Suggestion : 2

Last Updated : 15 Jul, 2022

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.
If start is not given in the syntax, it is assumed to be 0.```

Possible two syntaxes:

```sum(a)
a is the list, it adds up all the numbers in the
list a and takes start to be 0, so returning
only the sum of the numbers in the list.
sum(a, start)
this returns the sum of the list + start```

Output:

```25
35```

Suggestion : 3

Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.,If axis is a tuple of ints, a sum is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before.,If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.,Sum of array elements over a given axis.

```>>> np.sum([])
0.0```
```>>> np.sum([0.5, 1.5])
2.0
>>>
np.sum([0.5, 0.7, 0.2, 1.5], dtype = np.int32)
1
>>>
np.sum([
[0, 1],
[0, 5]
])
6
>>>
np.sum([
[0, 1],
[0, 5]
], axis = 0)
array([0, 6]) >>>
np.sum([
[0, 1],
[0, 5]
], axis = 1)
array([1, 5]) >>>
np.sum([
[0, 1],
[np.nan, 5]
], where = [False, True], axis = 1)
array([1., 5.])```
```>>> np.ones(128, dtype = np.int8).sum(dtype = np.int8) -
128```
```>>> np.sum([10], initial = 5)
15```

Suggestion : 4

Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.,This can be controlled with the min_count parameter. For example, if you’d like the sum of an empty series to be NaN, pass min_count=1.,If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series.,The required number of valid values to perform the operation. If fewer than min_count non-NA values are present the result will be NA.

```>>> idx = pd.MultiIndex.from_arrays([
...['warm', 'warm', 'cold', 'cold'],
...['dog', 'falcon', 'fish', 'spider']
],
...names = ['blooded', 'animal']) >>>
s = pd.Series([4, 2, 0, 8], name = 'legs', index = idx) >>>
s
blooded animal
warm dog 4
falcon 2
cold fish 0
spider 8
Name: legs, dtype: int64```
```>>> s.sum()
14```
```>>> pd.Series([], dtype = "float64").sum() # min_count = 0 is the
default
0.0```
```>>> pd.Series([], dtype = "float64").sum(min_count = 1)
nan```
```>>> pd.Series([np.nan]).sum()
0.0```
```>>> pd.Series([np.nan]).sum(min_count = 1)
nan```