user13963099
user13963099

Reputation:

Writing an array with a range in Python3.x

Let's assume I want to write a row in a csv file. This row will be something like:

name,last_name,birthday,hobby1,hobby2,hobby3, ... , hobby200 

I can create an array like ['name', 'last_name' .... ] but is there any possible way to set a range o

Upvotes: 3

Views: 81

Answers (4)

Riccardo Bucco
Riccardo Bucco

Reputation: 15364

Here is a possible solution:

result = 'name,last_name,birthday,' + ','.join(f'hobby{i}' for i in range(1, 201))

Instead, if you want a list:

result = ['name', 'last_name', 'birthday'] + [f'hobby{i}' for i in range(1, 201)]

Upvotes: 2

12ksins
12ksins

Reputation: 307

I assume you want this

List = ['name', 'last name', 'birthday']
List = List + ['hobby%d' %i for i in range(200)]
print(List)

output

['name', 'last name', 'birthday', 'hobby0', 'hobby1', 'hobby2', ....., 'hobby199']

if you don't want 0, and want 200 do range(1, 201)

Upvotes: 3

Alexanderbira
Alexanderbira

Reputation: 444

From what I can tell, you're trying to convert:

['name', 'last_name' .... ]

into a string:

name,last_name,birthday,hobby1,hobby2,hobby3, ... , hobby200 

Here's how you convert an array into a comma-separated string:

myList = ['name', 'last_name', 'birthday']
myString = ','.join(myList)
print(myString)

Upvotes: 1

Gledi
Gledi

Reputation: 99

I still do not fully understand what you mean. But try this:

row = ['name','last_name','birthday']
for hobby_number in range(1, 201):
    row.append('hobby' + str(hobby_number))
print(row)

Upvotes: 1

Related Questions