Tuple Sums (Solution)
00:00 Let’s start with creating the data tuple. I’m going to open and close parentheses, and then inside of here I can create two more tuples that are each going to contain two integers, so I’m making another set of parentheses, and then inside of that set of parentheses, I’m putting data just one and two.
00:19 Then I will put a comma after and then create the next element of the data tuple, which is another tuple. And then this one contains the integers three and four, separated by commas again, so we have
00:34
data tuple. And the data tuple also contains two tuples. So if I use the type() function and pass in data[0], for example. So accessing the first tuple in my nested tuple.
00:50 Then this is also a tuple, so we’ve got a nested tuple called data.
00:56 The next task was to loop over data and print the sum of each nested tuple in there. Okay, this is kind of how I remember the task.
01:11 We’re going to need a for loop, but I want to keep track of the index of the data. So I’m going to start off by creating an index variable that I’ll start with one, and then I’m writing the for loop for each row in the data tuple, I want to print an f-string, so it’s just a nice way of formatting it.
01:38 And then here, I think it was saying row, and then the row number, which is going to be the index,
01:47
and then the sum column. And then here I need to sum the values in the tuple, which you can do by using the built-in sum() function. So I can use sum() and then pass in the row.
02:01 So this row is the loop variable,
02:05
and that’s going to take on the values of first, the first tuple, and then the second tuple. And summing all elements in each of those rows using, using this sum() opening parentheses, and then passing in the loop variable row.
02:20
Okay, that should be the string that I want to print. Then I still need to increase the index. So I’m going to say index += 1 so that then, it’s going to say row number two when it goes the second time through the loop, let’s try it out.
02:37 Row one, sum three and row two sum seven. The output looks just like it should. Perfect.
02:45
Note that there’s also another way of solving this using the built-in enumerate() function. Maybe you’ve heard about it, maybe not. If not, and you’re happy with the solution that’s similar to the one before where you define an index variable, then that’s completely fine.
03:00
But I wanted to show you this enumerate() function too, because it makes the code look a little more Pythonic. I would say. You can say for row_number, , row in enumerate(data), and then I have to define that I want to start at one because otherwise it starts at zero by default.
03:24 And now I can directly print out,
03:29 I have access to both the row number as well as the row loop variable here. You could also keep calling that index, but I feel like row number is a bit more descriptive.
03:41
So print that out. And that’s it actually. So you don’t need to work with an index variable, and you don’t need to increment the index variable because enumerate() does that for you.
03:51
What enumerate() does is it returns a tuple, and then over here inside of your for loop, you are actually unpacking that tuple gaining two values.
04:00
First, a counter that starts at one. This is what you defined by saying start=1, and then the element enumerate() wraps these two things into a tuple.
04:10
And here you’re unpacking that tuple so that you have access to both of those, and you can use them in your loop. In this case, just for the f-string for your call to print().
04:20
And as you can see, the output is the same, but the code is shorter and a little easier to reason about, at least if you’ve heard about the enumerate() function before.
04:31 Now you have. So hope that’s a useful addition to your tool belt.
Enzo Savelli on March 7, 2025
If I use the first solution it returns me this error.
Traceback (most recent call last):
File "<pyshell#24>", line 2, in <module>
print(f"Row {index} sum: {sum(row)}")
TypeError: 'tuple' object is not callable
Bartosz Zaczyński RP Team on March 9, 2025
@Enzo Savelli Did you declare a tuple variable named sum by any chance?
The error message says you’re trying to call a tuple object, and the only part of the erroneous line where you call something is the call to sum(row). So, if you declared a variable named sum, it’d shadow Python’s built-in sum() function.
Ismar on Nov. 14, 2025
It’s funny how you say the version with enumerate is shorter when it very clearly is not 😄. Still, I appreciate you showing enumerate here as well!
Martin Breuss RP Team on Nov. 15, 2025
@Ismar ha, yeah I guess it depends how you look at it… if you only focus on characters then you’re right, it’s not shorter. But I’d argue it’s easier to reason about, and more robust, because it’s harder to mess up what I did with index in the first solution.
Either way, it’s a nice pythonic solution to keep around in your toolbelt : )
Ismar on Nov. 15, 2025
@Martin (why can you tag me but I can’t tag you?) that link you shared is awesome! Can’t wait to do that learning path. Sadly I can’t bookmark it like other content on the site.. thanks for sharing!
And yes, I meant “shorter” in the pure sense of the word, which in this context is literally the number of characters the whole construct occupies on screen. Definitely can’t argue with your points about ease of reasoning, robustness and pythonicness (!?😂).
Martin Breuss RP Team on Nov. 17, 2025
@Ismar I also can’t tag you actually, I’m just bolding your username, the comments here don’t have a tagging system 😅
Right, we don’t have bookmarks for Learning Paths at the moment, but you can view them all in the Learning Paths page. There’s completion tracking, so you’ll see how much you did of any path right there on the site!
For a fun (and entirely obsolete) philosophical detour, I can imagine that there are lots of ways to think about what “shorter” could mean (ah, the ambiguity of language…). For example, the second solution has one fewer line of code (2 lines vs. 3 lines in the first one), so it’s… shorter vertically, but longer horizontally? 🤪 But detour aside, you’re absolutely right that there are better ways to describe that refactoring than saying that it’s shorter :)
Become a Member to join the conversation.

ivan.dimitri on Sept. 16, 2024