Python Forum

Full Version: Make an array of string number in a List
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I just started to learn programming using Python, and I was wondering if anyone could help me with a solution to make an array of strings containing numbers in a list:

[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’,… until ‘100’]

is it possible to use a compact List Comprehension?

thanks

Dj
Hi, We can use numpy to create an array from the list
import numpy as np
my_list = [2,4,6,8,10]
my_array = np.array(my_list)
print(my_array)
Output:
[ 2 4 6 8 10]
Just wanted to be more clear about your requirement?
(May-27-2020, 03:21 AM)polantas Wrote: [ -> ]is it possible to use a compact List Comprehension?
yes, it's possible and very convenient
Look at range() and str().
Try to come with solution yourself
got it! :)
str(number) for number in range (1, 101)
x = []
for i in range(1,101):
	x.append(str(i))
	if (i) == 100:
		print(str(x))
@DOS - 5 lines vs one-liner list comprehension.... Also, no need to use if block, nor have brackets around i, no need to convert x to str for printing, finally single char names are considered bad practice
So if you insist on expanding list comprehension
my_list = []
for number in range(1, 101):
    my_list.append(str(number))
print(my_list)