-1

I need your help:

I want to create a list looking like this ['Unnamed: 16', 'Unnamed: 17', 'Unnamed:18'] for a range (16,60). How can I proceed?

I don't know if my question is clear but it's like doing list(range(16, 60) but with a string before each numbers.

Thank you very much for your help!!

Titouan L
  • 1,182
  • 1
  • 8
  • 24

5 Answers5

3

You can use f-strings to do so :

my_list = [f"Unnamed: {i}" for i in range(16, 60)]

# Output
['Unnamed: 16', 'Unnamed: 17', 'Unnamed: 18', 'Unnamed: 19', ...]
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
Titouan L
  • 1,182
  • 1
  • 8
  • 24
0

I would do it following way

prefix = "Unnamed: "
lst = [prefix + str(i) for i in range(16,25)]
print(lst)

output

['Unnamed: 16', 'Unnamed: 17', 'Unnamed: 18', 'Unnamed: 19', 'Unnamed: 20', 'Unnamed: 21', 'Unnamed: 22', 'Unnamed: 23', 'Unnamed: 24']

Note: I used othre range for brevity sake. You might elect to use one of string formatting method instead.

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

You can do it using map as,

list(map(lambda x: f'Unnamed: {x}', range(16, 60)))
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
0

You can use f strings

name = 'Unamed:'
list = [f"{prefix} {i}" for i in range(16, 60)]
print(list)
Kermit
  • 89
  • 5
-1
my_list = []

for i in range(16, 60):

    my_list.append("Unnamed: " + str(i))

print(my_list)
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 25 '22 at 11:49