Python Forum
Make an array of string number in a List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make an array of string number in a List
#1
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
Reply
#2
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?
Reply
#3
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
got it! :)
str(number) for number in range (1, 101)
Reply
#5
x = []
for i in range(1,101):
	x.append(str(i))
	if (i) == 100:
		print(str(x))
Reply
#6
@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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Extracting Version Number from a String britesc 2 1,030 May-31-2023, 10:20 AM
Last Post: britesc
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,258 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,013 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  help me to make my password list in python >>> Oktay34riza 0 552 Dec-23-2022, 12:38 PM
Last Post: Oktay34riza
  prefix ID Number with 0,00 make 3 digit. mg24 1 708 Oct-06-2022, 07:20 AM
Last Post: ibreeden
  functional LEDs in an array or list? // RPi user Doczu 5 1,521 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,763 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,197 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 7,926 Feb-12-2022, 01:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020