Modifying all the tuples in a Python list

I have a list containing tuples with a standard format:

bar_list = [(bar1, bar2, bar3, bar4), (bar1, bar2, bar3, bar4), (bar1, bar2, bar3, bar4)...] 

Though I want to iterate through each tuple in the list and for each make specific modifications such as:

foo0 = bar1
foo1 = get_foo(foo0) #get_foo(var) being a function
foo2 = bar2
foo3 = bar3/2

And then repackage the revalued tuples in another list:

foo_list = [(foo1, foo2, foo3), (foo1, foo2, foo3), (foo1, foo2, foo3)...]

How could I accomplish this?

Answers


You could use a list comprehension:

foo_list = [(get_foo(bar1), bar2, bar3/2) 
            for bar1, bar2, bar3, bar4 in bar_list]

Note, I'm assuming you meant for foo1 to equal get_foo(bar1) rather than the NameError-raising and self-referential

foo1 = get_foo(foo1)

Need Your Help

Prism - Bad idea to use it?

wpf prism mef

I have been migrating from Windows Forms to WPF and am trying to utilize Prism to meet the requirement that the application be plug-in oriented.

Make a text Highlight using javascript

javascript html regex

I want to make a word bold in given paragraph. Here is a javascript code.

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.