Python Forum
Make an array of string number in a List - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Make an array of string number in a List (/thread-27126.html)



Make an array of string number in a List - polantas - May-27-2020

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


RE: Make an array of string number in a List - KavyaL - May-27-2020

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?


RE: Make an array of string number in a List - buran - May-27-2020

(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


RE: Make an array of string number in a List - polantas - May-27-2020

got it! :)
str(number) for number in range (1, 101)


RE: Make an array of string number in a List - DOS - May-27-2020

x = []
for i in range(1,101):
	x.append(str(i))
	if (i) == 100:
		print(str(x))



RE: Make an array of string number in a List - buran - May-27-2020

@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)