8

I have a list:

Student_Grades = ['56', '49', '63']

and I want to convert each of the entries to integers so that I can calculate an average.

Here's my code for converting:

for i in Student_Grades:
    Student_Grades = [int(i)]

I keep getting the error

invalid literal for int() with base 10: '56,'

and I don't know what to do.

Here is my full code on how I got Student_Grades Choose_File = str(input("Please enter the exact name of the file to be read in (including file extention) : "))

with open(Choose_File, "r") as datafile:
    counter = 1
    x = 1
    Student_Grades = []
    Read = datafile.readlines()
    info = Read[counter]
    Split_info = info.split()
    n = len(Split_info)


    while x < n:
        Student_Grades.append(Split_info[x])
        x = x + 2

The textfile has the format 'MECN1234 56, MECN1357 49, MATH1111 63'

Quintec
  • 1,064
  • 12
  • 22
swami_108
  • 119
  • 1
  • 1
  • 8
  • 3
    Your list does *not* contain just `'56'`; there is a comma there too. How did you *create* `Student_Grades`? – Martijn Pieters Apr 29 '14 at 21:27
  • I read in a few lines from a textfile with the format: MECN1234 56, MECN1357 49, MATH1111 63 I then split the lines using .split() and then appended the Student_Grades list with every 2nd entry from the split list (which were the grades). – swami_108 Apr 29 '14 at 21:31
  • Here's my full code: Choose_File = str(input("Please enter the exact name of the file to be read in (including file extention) : ")) with open(Choose_File, "r") as datafile: counter = 1 x = 1 Student_Grades = [] Read = datafile.readlines() info = Read[counter] Split_info = info.split() n = len(Split_info) while x < n: Student_Grades.append(Split_info[x]) x = x + 2 – swami_108 Apr 29 '14 at 21:32
  • @user3576929 larger pieces of codes is better to add to your question (if it does not change your question), it is much more readable. – Jan Vlcinsky Apr 29 '14 at 21:35
  • How would I deal with that comma? – swami_108 Apr 29 '14 at 21:45
  • This will solve your problem in 1 line using list comprehension test = 'MECN1234 56, MECN1357 49, MATH1111 63' numbers = [int(entry.split()[-1]) for entry in test.split(',')] – Hua Chi Quan May 19 '23 at 09:01

3 Answers3

21

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

ascripter
  • 5,665
  • 12
  • 45
  • 68
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
12

You should do this:

for i in range(len(Student_Grades)):
    Student_Grades[i] = int(Student_Grades[i])
Quintec
  • 1,064
  • 12
  • 22
8
In [7]:

Student_Grades = ['56', '49', '63']
new_list = [int(i) for i in Student_Grades]
print(new_list)
[56, 49, 63]
xbb
  • 2,073
  • 1
  • 19
  • 34