Python Forum

Full Version: How do I add a number to every item in a list?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey Guys,

I;m wondering how could I add a number to every single item in a list.

So for example. test1, test2 test3, test4

If i would have a range from 1 to 9

and add it to words like: test, number, pencil, etc.

I would like to see

test1, test2, test3, test4, test5, test6

number1, number2, number3, number4, number5, number6.

I would like this as an input and not printing it out...

Could you please help me out?

kind regards,

John
I'm not sure I'm clear on what you're asking but here's a quick example of appending an integer to a string and generating a list of the results.

word_in = input("Enter a word to append to: ")
range_in = input("Enter the number of appended words: ")

res = []

for n in range(int(range_in)):
    res.append(word_in + str(n))

print(res)
I don't understand what you mean by "I would like this as an input and not printing it out." so this may not be what you want.
pencils = [f'pencil{a}' for a in range(1, 10)]
#or
tests = ['test'+str(a) for a in range(1, 10)]
#or
numbers = []
for a in range(1, 10):
    numbers.append('number'+str(a))