Home > Software design >  Adding prefix to existing array
Adding prefix to existing array

Time:01-17

I am trying to add suffix to an existing array. Below is my code

print('a'   [10, 100])

With this I am getting below error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "list") to str

Could you please help hoe to do that? I could use some for loop but I believe there may be more straightforward way to achieve the same.

CodePudding user response:

You can create a new concatenated array as:

>>> ['{0}{1}'.format('a', num) for num in [10, 100]]
['a10', 'a100']

Read String format and List Comprehensions from doc.

CodePudding user response:

If I understand your question, you want a new string array (list). You could try this:

new_lst = ['a' str(x) for x in [10, 100]] # just use string concatentation
  •  Tags:  
  • Related