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?
#1
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
Reply
#2
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)
Reply
#3
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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,507 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,177 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,837 Jul-01-2022, 01:23 PM
Last Post: deanhystad
Question Finding string in list item jesse68 8 1,856 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  Split a number to list and list sum must be number sunny9495 5 2,276 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 7,993 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 3,065 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  how to easily create a list of already existing item CompleteNewb 15 3,519 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Remove an item from a list contained in another item in python CompleteNewb 19 5,650 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  count item in list korenron 8 3,429 Aug-18-2021, 06:40 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

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