3

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

2
  • 2
    WDYM by "set a range"? Could you edit your question to include the outcome you'd expect? Commented Jul 21, 2020 at 9:48
  • This is the outcome I expect: name,last_name,birthday,hobby1,hobby2,hobby3, ... , hobby200 Commented Jul 21, 2020 at 9:52

4 Answers 4

3

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)

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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)

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.