0

I am trying to create a list which consists of numbers, however, when I convert them they turn individual digits into strings which I don't want that. I want only before and after the commas to be a string.

Please have a look at my code below:

numbers = list(range(1,100))
print(numbers)

Here is the result of the list:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,96, 97, 98, 99, 100]

So, I want it to like the following:

['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14','15', '16', '17', '18', '19', '20', '21', '22',.....]

How can I do that?

Mojo
  • 19
  • 8

3 Answers3

0

You can do this with a list comprehension:

numbers = [str(num) for num in ragne(1,100)]

Or using map:

numbers = map(str, range(1, 100))

Kevin Welch
  • 1,488
  • 1
  • 9
  • 18
0

Using a list comprehension, you can iterate over them returning the string value with the str() function

[str(n) for n in numbers]
nageeb
  • 2,002
  • 1
  • 13
  • 25
0

use a list comprehension

new = [str(n) for n in numbers]

Derek Eden
  • 4,403
  • 3
  • 18
  • 31