For string we can have
msg = 'successfully returned with msg: %s'
msg %= "Elegant";
Can we do the same for list,
For instance ,
cmd = [
"Message %s",
"Param %s"
]
cmd %= (msg, param)
You can use zip() to pair the two lists in a new list comprehension:
>>> cmd = [
... "Message %s",
... "Param %s"
... ]
>>> msg = "foo"
>>> param = "bar"
>>> newcmd = [item % par for item,par in zip(cmd, (msg,param))]
>>> newcmd
['Message foo', 'Param bar']
Try something like that:
>>> a = ['aaa %s', 'bbb %s']
>>> b = ['xxx', 'yyy']
>>> map(lambda x, y: x % y, a, b)
['aaa xxx', 'bbb yyy']
b is not list in my caseYou can use a list comprehension with zip():
cmd = [s % (value,) for s, value in zip(cmd, (msg, param))]
to apply the various values to list elements individually.
This produces a new list; assign back to cmd, or a different name, take your pick.
TypeError. You need zip() in order to correctly pair the two lists/tuples.My suggestion is that you itterate over your list elements and perform appending at that level. Here is my implementation:
cmd = [
"Message %s",
"Param %s"
]
aList = ["MyMessage","MyParam"]
for i in range(len(cmd)):
cmd[i] %= aList[i]
print(cmd)
==> ['Message Hello', 'Param MyParam']