#Write a program to replace the last values of all tuples in a list to 100
lst = [(10,20,30),(40,50,60),(70,80,90)]
print("the original list",lst)
new_lst = [tpl[:-1] + (100,) for tpl in lst]
print("the list with replaced values",new_lst)
Asked
Active
Viewed 90 times
1

Randy
- 14,349
- 2
- 36
- 42

Shreyash Kashid
- 27
- 5
-
because tuple takes less space than list and we need a one-element iterable that can be added to list here – h4z3 Jul 20 '21 at 14:34
-
If you used `+ 100` it would be trying to add a `tuple` to an `int`, the syntax `(100,)` makes a `tuple` with a single `int` element which will then be concatenated – Cory Kramer Jul 20 '21 at 14:34
1 Answers
2
Because parentheses define order of operations in Python (100)
would just be interpreted as the int
literal 100
and would throw an error:
In [1]: (10, 20, 30) + (100)
TypeError: can only concatenate tuple (not "int") to tuple
Adding the comma tells the Python interpreter that this is intended to be a tuple:
In [2]: (10, 20, 30) + (100,)
Out[2]: (10, 20, 30, 100)

Randy
- 14,349
- 2
- 36
- 42
-
Parentheses are *only* used for order of operations. `10, 20, 30, + 10,` would work fine. The only time you need parentheses for a tuple is if the comma that creates the tuple could be parsed as some other syntactic construct. (Or if the lack of a comma could change the semantics; `10, 20, 30 + 10,` would be the tuple `(10, 20, 130)`, since `30 + 10` would be considered an expression defining the 3rd element. `(10, 20, 30) + 10,` would avoid that.) – chepner Jul 20 '21 at 14:38
-
The only tuple that isn't created with a comma is the empty tuple, since `()` by itself is not a valid expression otherwise (and arguably looks better than `(,)`). – chepner Jul 20 '21 at 14:39
-
Would need to dig into the guts of CPython to say for sure whether that's true or whether parens get explicitly mapped to a call to `tuple` under some paths, but I will at least point out that `(10, 20, 30) + 10,` also throws a `TypeError` – Randy Jul 20 '21 at 14:51
-
Parens never get mapped to a call to tuple. Parens are used to guide the construction of the parse tree. (Apologies; `(10, 20, 30) + 10,` is indeed an error, again because it is parsed the same as `(10,20,30) + 10),`, not because parentheses having anything to do with non-empty tuples.) – chepner Jul 20 '21 at 14:54
-
1https://docs.python.org/3/reference/expressions.html#parenthesized-forms confirms the use of a comma to create a tuple. Also https://docs.python.org/3/reference/expressions.html#expression-lists. The one-element tuple is the only one requiring a *trailing* comma (since it is the *only* comma); trailing commas are otherwise optional. `x,y` and `x,y,` define the same tuple. – chepner Jul 20 '21 at 14:57
-
Fair - that doc is pretty explicit about `,` vs parens, and so I guess `() == tuple()` is just a bit of hefty syntactic sugar rather than an explicit call. I still don't see situations where the mental model of the parens -> tuple logic breaks down much but I'll edit the answer anyhow for correctness' sake. – Randy Jul 20 '21 at 15:01
-
It really only matters in the rare corner case of singleton tuples. If you think (correctly) that it's the comma that makes a tuple, you might mistakenly write `(,)` for the empty tuple, which just produces an easily fixed syntax error. But you think it's the parentheses that create a tuple, `("foo")` looks like it should be the tuple `("foo",)`, but it's *not* a syntax error, and because both are iterable, tracking down the error could be much more difficult, because you won't necessarily get a convenient `TypeError` right away. – chepner Jul 20 '21 at 15:18