hi all

i have list like
list1=[2,3,4]
m = 'ma'
m = list1 * m

wen i run the script i'm getting output like
TypeError: can't multiply sequence by non-int

i need a output like
[ma2,ma3,ma4]


so plzzzzz help me:(

Recommended Answers

All 5 Replies

You can use string formating operations

["ma%d" % n for n in range(2, 5)]

thanks for a reply Gribouillis

wen i run this ["ma%d" % n for n in range(2, 5)] i m gettin error:
SyntaxError: invalid syntax

You typed wrong, but the way you typed in your message is correct, try again!

IDLE 2.6.6      
>>> ["ma%d" % n for n in range(2, 5)]
['ma2', 'ma3', 'ma4']
>>>

(If you copy code to message push first (CODE) or paint the code and push it afterwards.)

You are expecting also wrong result, here is generic way to do your result, but the output is different:

import itertools

list1=[2,3,4]
m = 'ma'
print(tuple('%s%s' % combination for combination in itertools.product(list1, m)))
""" Output (not what you expected, but logically correct):
('2m', '2a', '3m', '3a', '4m', '4a')
"""

Actually
[ma2, ma3, ma4]
would imply a list of variable names.
You have to let Python know what those variable names represent.

Yes Vegas eater, but expecting variables to come out of the product would be little much.

If the OP, would open his heart and share his dreams, which he want to accomplish together with Great Python, maybe we could help to open the Gate of Heaven ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.