python - can't sum my list because of commas -


i have list of numbers input user. list of random length depending on how many elements user inputs. python sees list (e.g) this: numbers entered are:

['6', '7', '8', '4', '5', '98', '34', '56'] 

they on list called numlist.

i want sum list - tried

print(sum (numlist) 

it won't sum, reckon, because of commas. believe makes them string items not list. so, think have remove commas, turning them list, can sum. i've looked over, can't find solution makes sense me. (sorry i'm quite new this. )

i've experimented strip. map. , int. can't find 1 job. i'm setting code incorrectly someplace. please help.

you trying add string elements of list. if want sum of list elements. e.g. if have list

a = ['1','2'] 

1) convert list elements of a int , assign list b

 b= [int(i) in ] 

2) sum list elements using sum

 print sum(b) 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -