0

Perhaps this has been answered, but I sincerely could not find it.

I wish to have a specific output in the form of:

("A_1", "A_2", ..., "A_100")

I tried:

a = "A_"
nums_1_100 = str(list(range(1,101)))
for i in range (1,101):
    x = a
    x += nums_1_100

And this returns:

'A_[1, 2, 3, 4, 5, ..., 100]'
1
  • Using map print(list(map(lambda x: 'A_%d' % x, range(1, 101)))) Commented Mar 25, 2018 at 15:45

3 Answers 3

2

Your code makes no sense as you overwrite x each iteration, which in result kills what your code produced in previous one. You want rather to use simple list comprehension instead:

result = [ 'A_%d' % i for i in range(1,101)]

which would then produce list with elements like A_1, A_2 ...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :) I suppose I could've known it didn't make sense, and thanks for the answer!
0

Try a list comprehension to make that list of strings like:

x = ['A_{}'.format(i) for i in range(1, 101)]

Comments

0

Here is the functional approach. This will work with a list of any type, so if you decided to switch from ints at the end to strings/characters, all that would need to change would be the contents of listToAppend.

listToAppend = list(range(1, 101))
output = list(map(lambda x: "A_" + str(x), listToAppend))

1 Comment

I am afraid that this is the type of approach we call "over-engeneering". As result, you will end up with the code that is universal for no real benefits, hard to understand and complicated for every single simple task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.