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
  Make Groups with the List Elements quest 2 775 Jul-11-2021, 09:58 AM
Last Post: perfringo
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 1,931 May-02-2021, 03:45 PM
Last Post: Anldra12
  Convert String of an int array to a Numpy array of ints mdsousa 5 2,151 Apr-08-2021, 08:00 PM
Last Post: mdsousa
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 1,260 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
Question How to make a 3D List of Excel Spreadsheets? chatguy 4 1,138 Jan-24-2021, 05:24 AM
Last Post: buran
  Regular expression: cannot find 1st number in a string Pavel_47 2 1,051 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  LIST or ARRAY Comparison and change of value nio74maz 0 760 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  Undo interation to make a single list? DustinKlent 2 930 Nov-29-2020, 03:41 AM
Last Post: DustinKlent
  How to make global list inside function CHANKC 6 1,309 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  2d Array adds last element to entire list waiteup 2 871 Nov-19-2020, 08:25 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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