Python Forum
How do I add a number to every item in a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I add a number to every item in a list?
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,

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

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)]
tests = ['test'+str(a) for a in range(1, 10)]
numbers = []
for a in range(1, 10):

Possibly Related Threads…
Thread Author Replies Views Last Post
  count item in list korenron 7 283 Jul-01-2021, 01:46 PM
Last Post: deanhystad
  Time.sleep: stop appending item to the list if time is early quest 0 356 Apr-13-2021, 11:44 AM
Last Post: quest
  How to run a pytest test for each item in a list arielma 0 485 Jan-06-2021, 10:40 PM
Last Post: arielma
  Count number of occurrences of list items in list of tuples t4keheart 1 560 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  Ignoring a list item hank4eva 2 713 Aug-17-2020, 08:40 AM
Last Post: perfringo
  Select correct item from list for subprocess command pythonnewbie138 6 904 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  Print the number of items in a list on ubuntu terminal buttercup 2 733 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Why is the item not in list when it is DanielCook 2 654 Jul-08-2020, 07:38 PM
Last Post: deanhystad
  Make an array of string number in a List polantas 5 1,077 May-27-2020, 07:18 AM
Last Post: buran
  best way to add item to list only once Phaze90 1 787 May-01-2020, 05:02 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020