Write a function sum_of_digits(A), that takes in a list of numbers A, and returns the sum of all the digits in the numbers.

e.g. [10, 20, 3, 5, 6, 23] should return 1 + 0 + 2 + 0 + 3 + 5 + 6 + 2 + 3 = 22.

e.g. [10, 20, 3, 5, 6, 23] should return 1 + 0 + 2 + 0 + 3 + 5 + 6 + 2 + 3 = 22.

```
"""
the solution isn't the best out there but it is good to guide beginners may be understand
Function sum_of_digits that returns sum of all digits in the list argument
@param list of numbers, the assumption is the list contains only numbers
@return the total of all numbers. e.g. sum_of_digits([10, 20, 3, 5, 6, 23])
should return 1 + 0 + 2 + 0 + 3 + 5 + 6 + 2 + 3 = 22
"""
def sum_of_digits(lst):
total = 0 # initialise total to 0
new_list = [] #create a new variable list
#iterate through the list
for a in lst:
#add the returned list to the new_list i.e. [] + ['1', '0'] for the 1st iteration
new_list += list(str(a))
#loop through the new_list
for number in new_list:
# add individual elements of new_list only after converting it to an integer int()
total += int(number)
#return the total
return total
print(sum_of_digits([10, 20, 3, 5, 6, 23]))
```

Hello

My intention is to hint about some of the concepts we introduced last week and introduce new methods like

`list("string") - which returns individual characters of a "string" i.e. ["s", "t", "r", "i", "n", "g"]`

and type-conversion methods like `str(2.6) - returns "2.6"`

, `int("2.6") - returns 2`

.
```
```

```
mylist = [10, 20, 3, 5, 6, 23]
def sum_digits(A):
total = 0
for a in A:
print a
b = str(a)
for c in b:
total += int(c)
print total
sum_digits(mylist)
```

A simple way of doing the sum_of_digits(A)

```
def sum_of_digits(A):
"""Write a function sum_of_digits(A), that takes in a list of numbers A,
and returns the sum of all the digits in the numbers.
e.g. [10, 20, 3, 5, 6, 23] should return
1 + 0 + 2 + 0 + 3 + 5 + 6 + 2 + 3 = 22
"""
b = list("".join(str(e) for e in A))
return sum([int(i) for i in b])
print sum_of_digits([20, 50, 60])
print sum_of_digits([100, 400, 323])
```

another way of doing it
@fnzisa good stuff.... yeah this is actually better, @Tonida please share your code and for guys that haven't done *list compression* please google about it

@fnzisa one other thing please comment code. It's best practice to comment code.

@andela-engmkwalusimbi Thank you.

@fnzisa, @Tonida Would you ladies solve this too and please try to explain to your friends understand what your code is doing.

```
Write a function sum_of_unique_digits(A), that takes in a list of numbers A, and returns the sum of all the unique digits in the numbers.
e.g. [10, 20, 3, 5, 6, 23] should return 1 + 0 + 2 + 3 + 5 + 6 = 17.
```

Has anyone worked on the factorial problem? There are a lot of implementations to it. I would prefer you work on without going on google, this will help broaden your way of thinking about problems. That said, I'm glad you all made it today.

```
def sum_of_unique_digits(A):
"""Write a function sum_of_unique_digits(A), that takes in a list of numbers A,
and returns the sum of all the unique digits in the numbers.
e.g. [10, 20, 3, 5, 6, 23] should return 1 + 0 + 2 + 3 + 5 + 6 = 17.
"""
b = list("".join(str(e) for e in A))
for i in b:
if b.count(i) > 1:
b.remove(i)
return sum([int(i) for i in b])
print sum_of_unique_digits([1, 20, 1, 30, 2])
print sum_of_unique_digits([0, 20, 1, 30, 2, 30])
print sum_of_unique_digits([100, 50, 20])
```

hi

```
x = {"a": "apple", "b": "Boy"}
for key, value in x.iteritems():
print key, value
```

```
def sum_of_unique_digits(A):
"""Write a function sum_of_unique_digits(A), that takes in a list of numbers A,
and returns the sum of all the unique digits in the numbers.
e.g. [10, 20, 3, 5, 6, 23] should return 1 + 0 + 2 + 3 + 5 + 6 = 17.
"""
""" convert A into a string using .join() function then use the list()
function to convert back to a list and store it in b
"""
b = list("".join(str(e) for e in A))
for i in b: # iterate through the elements in b
if b.count(i) > 1: # if an element appears more than once
b.remove(i) # use remove function to delete from the list
return sum([int(i) for i in b]) # compute the sum of elements in b converted to integer type
```

the commented code on sum of unique digits
@Tonida what does this line do?

`b = list("".join(str(e) for e in A))`

@andela-engmkwalusimbi I have written in the comment above that line

Sum of digits with comments added

```
mylist = [10, 20, 3, 5, 6, 23]
def sum_of_digits(A):
#initialise total to 0
total = 0
#iterate through through the list
for a in A:
#Converts each element of A to a string at a time and assigns it to b. Example: 10 will be '10'
b = str(a)
#Loops through every element of b
for c in b:
#adds every element of b to the total after converting it to integer
total += int(c)
#print the total
print total
sum_digits(mylist)
```

@Tonida seen. I'm a beginner. Would you please use a scenario to explain to me. Thank you :smile:. I mean give me an example for just that line :+1: .

@fnzisa, @Tonida appreciate the effort and hard work.... please explain to your colleagues. I don't mean it's just @fnzisa and @Tonida that are nailing it, everyone is. You all will progress when you discuss.

COLLABORATE

COLLABORATE

check out this link, it explains classes very well

https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/

https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/

:thumbsup:

and this https://repl.it/Bxlp/17