+ 1
[Solved] Can you explain why the argument's appended as a whole tuple?
def f(*y): x.append(y) return x x = [3, 7] f(2, 3, 4) print(x) out: [3, 7, (2, 3, 4)] and not [3, 7, 2, 3, 4] Thanks. [edit; question before was: Can you explain why the tuple is appended as a whole after getting unpacked in the args? (py) I had to change the wording for more accurate search results.]
6 Réponses
+ 7
Because the arguments when given in the *args format is a tuple. And when you append a tuple, it doesn't append the values seperately.
https://code.sololearn.com/cimc94VivtPT/?ref=app
+ 3
That's due to the *y argument in the function which converts the values you've written in f() as a tuple
To correct this and print the values separately you can simply use the for loop inside the function and indent the append function inside the loop, such as this :
def f(*y)
for value in y:
x.apend(value)
return x
And your function is done 😁
+ 3
Zen Ai Thank you.
I severely mixed up stuff here. Naming the f arg in my mind already as a tuple: wrong
Then treating that as a generator, then passing each of its elements one by one, wrong again. Lol.
I have to delete the "unpack" tag, it is misleading for people at my level or more novice.
+ 3
Korkunç el Gato continues trials and errors and corrections of those errors leads to mastery✨✊
+ 2
Slick Thanks a lot. Cure to my confusion.
+ 2
*y still saves the arguments in tuple format