-2

that's a basic one but I'm stuck.

I want to create a list with combined strings and numbers like [df_22, df_23, ... df_30].

I have tried

def createList(r1, r2):
   return str(list(range(r1, r2+1)))

so it gives me a list of numbers:

In: mylist = createList(2, 30)
In: mylist
Out: '[22,23, 24, 25, 26, 27, 28, 29, 30]'

I am not sure how to add 'df_' to it because 'return df + str(list(range(r1, r2+1)))' gives an UFuncTypeError.

and

def createList(r1, r2):
   res = 'df_'
   return res + str(list(range(r1, r2+1)))

In: mylist = createList(22, 30)
In: mylist
Out: 'df_[22, 23, 24, 25, 26, 27, 28, 29, 30]'

I need my list to be

mylist

Out: [df_22, df_21, ... df_30]

Bluetail
  • 1,093
  • 2
  • 13
  • 27
  • 2
    `mylist = [df_22, df_21, ... df_30]` is not valid Python (unless all of those df things have already been defined as variables). – Scott Hunter Jul 10 '21 at 21:30

1 Answers1

1

This does what your title seems to describe:

def createList(r1, r2):
    return str(['df_%d'%x for x in range(r1, r2+1)])
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • thank you - my background is in finance and statistics not in programming. so yes I have amended my question after I have read your comments. – Bluetail Jul 10 '21 at 21:37
  • can you explain what '%x ' means in your solution? – Bluetail Jul 10 '21 at 21:38
  • 2
    [Old string formatting](https://docs.python.org/3/tutorial/inputoutput.html#old-string-formatting) from the python docs. Also [What does % do to strings in Python?](https://stackoverflow.com/q/1238306/15497888) – Henry Ecker Jul 10 '21 at 21:47