-2

I have a list in txt file and I want to remove the quotation marks from the numbers. For example the list:

['1023', '1025', '1028']

change to

[1023, 1025, 1028]

How do I do it in Python?

michailk
  • 1
  • 1

3 Answers3

2

We can use a list comprehension here along with int() to convert each string in the list to an integer.

inp = ["1023", "1025", "1028"]
output = [int(x) for x in inp]
print(output)  # [1023, 1025, 1028]
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Try:

lst  = ['1023', '1025', '1028']
list(map(int, lst))
0

The easiest way is probably:

Your list:

a = ['1023','1025','1028']

And change it to a NumPy array:

import numpy as np

a = np.array(a).astype(float)

Out: array([1023., 1025., 1028.])

Or astype(int) depending on if you want items after the decimal point.

Out: array([1023, 1025, 1028])
Matt
  • 2,602
  • 13
  • 36
  • Installing third-part libraries to do something this trivial is overkill. And it changes the output format in a different way to boot. – ShadowRanger Sep 02 '22 at 10:51
  • Well just about everyone with Python has NumPy and it makes it easier to do any math operations if needed. I have no idea what this person is doing with the numbers. – Matt Sep 02 '22 at 12:50