-7

I have a list that looks something like this: days_list = ['12','554','43','2343','52']

How do I modify it so that it looks like this: days_list = ['days:12', 'days:554', 'days:43', 'days:2343', 'days:52']?

LQQ
  • 67
  • 6
  • days:12 value is a mix of string and int type.. – Anjaly Vijayan Sep 24 '20 at 03:34
  • It was just an example. I'll edit it. – LQQ Sep 24 '20 at 03:40
  • Please show what attempts you made to resolve this. A simple google search returned the link for another stack overflow question asked over 10 years ago : https://stackoverflow.com/questions/2050637/appending-the-same-string-to-a-list-of-strings-in-python. Please edit your question if it's something different from that. – Amit Singh Sep 24 '20 at 06:25
  • 3
    Does this answer your question? [Appending the same string to a list of strings in Python](https://stackoverflow.com/questions/2050637/appending-the-same-string-to-a-list-of-strings-in-python) – Amit Singh Sep 24 '20 at 06:26

3 Answers3

0

Make a new list, based on the old, where each element is a string with prefix "days:" and then the original value.

days_list = ['days:{}'.format(value) for value in days_list]

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

Using list comprehension you can do this easily

result = ['days:'+item for item in days_list]
Anjaly Vijayan
  • 237
  • 2
  • 9
0

Write the code as:

days_list = ['days:{}'.format(value) for value in days_list]

or

days_list = [f'days:{value}' for value in days_list]

or

days_list = [f'days:{days_list[x]}' for x in range(0, len(days_list))