Converting list items from string to int(Python)
Apply int
on each item in the list and return it as a list:
>>> StudentGrades = ['56', '49', '63']
>>> res = list(map(int, StudentGrades)) # this call works for Python 2.x as well as for 3.x
>>> print res
[56, 49, 63]
Note about map
differences in Python 2 and 3
In Python 2.x map
returns directly the list, so you may use
>>> res = map(int, StudentGrades)
but in Python 3.x map
returns an iterator, so to get real list, it must be wrapped into list
call:
>>> res = list(map(int, StudentGrades))
The later way works well in both version of Python
You should do this:
for i in range(len(Student_Grades)):
Student_Grades[i] = int(Student_Grades[i])
In [7]:
Student_Grades = ['56', '49', '63']
new_list = [int(i) for i in Student_Grades]
print(new_list)
[56, 49, 63]