Welcome to Serpia's blog!
Blog Entry
Tupleware
Removing items from a tuple and rebuilding the tuple using the builtin zip function
You have a tuple like:mytuple = [('harry',60,5),('susan',35,3),('guy',28,9),('peter',56,1)]
and want to turn it into:
newtuple = [('harry', 5), ('susan', 3), ('guy', 9), ('peter', 1)]
Solution: Explanation: is a short and smart way of doing the following: This is perhaps easier to understand: every first row (remember programmers start counting by 0) in 'mytuple' is appended to the list 'names'. The concept of rows in tuples is clear when we change the notation of mytuple: The same thing happens to 'cats' of course, and finally uses zip which is a builtin function that returns a list of tuples where each tuple contains the i-th element from each of the argument sequences.
Comments
Post a comment