python - Replacing last value in list of tuples -
so, have tuple this:
a=[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
and want replace last value of each tuple 100. can do:
b=[(t[0],t[1],) + (100,) t in a]
that gives me this:
[(1, 2, 100), (4, 5, 100), (7, 8, 100)].
what's shortcut? these tuples, in real, have 50 elements in it?
use tuple slicing:
[t[:-1] + (100,) t in a]
there no need build new tuple individual elements here.
Comments
Post a Comment