-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!!

3
  • 3
    [f"whatever {i}" for i in range(...)] Commented May 25, 2022 at 10:00
  • First of all you didn’t format your code properly you have a missing bracket. Also don’t ask how to write code, see “How to ask Questions”. Instead show what you have tried. Commented May 25, 2022 at 10:03
  • stackoverflow.com/questions/29339525/… Commented May 25, 2022 at 10:03

5 Answers 5

4

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', ...]
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

You can do it using map as,

list(map(lambda x: f'Unnamed: {x}', range(16, 60)))

Comments

0

You can use f strings

name = 'Unamed:'
list = [f"{prefix} {i}" for i in range(16, 60)]
print(list)

Comments

-1
my_list = []

for i in range(16, 60):

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

print(my_list)

1 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.