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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -