Replacing last value in list of tuples

So, I have a tuple like this:

a=[(1, 2, 3), (4, 5, 6), (7, 8, 9)] 

and I want to replace last value of each tuple with 100. So I can do:

b=[(t[0],t[1],) + (100,) for t in a]

That gives me this:

[(1, 2, 100), (4, 5, 100), (7, 8, 100)].

What's the shortcut? These tuples, in real, have 50 elements in it?

Answers


Use tuple slicing:

[t[:-1] + (100,) for t in a]

There is no need to build a completely new tuple from individual elements here.


Need Your Help

ASP.NET MVC 5 / Metadata services - DataAnnotations - is this still the preferred way?

asp.net-mvc data-annotations modelmetadataprovider

I haven't followed the ASP.NET MVC evolution since version 3 or so. Back then in MVC3 the simplest/preferred way to boost entity metadata info for the MVC DisplayNameFor etc magic was to use

.htaccess Redirect / Rewrite with wildcards

wordpress .htaccess redirect url-redirection http-redirect

Over the past year I have been using a WordPress theme that has forced me to incorporate a series of 301 redirects in order to allow users to view subsequent page through the page navigation. That...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.