[Solved] Can you explain why the argument's appended as a whole tuple? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.]

4th Sep 2022, 10:02 PM
Korkunç el Gato
Korkunç el Gato - avatar
6 Answers
+ 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
4th Sep 2022, 10:24 PM
Slick
Slick - avatar
+ 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 😁
4th Sep 2022, 10:43 PM
Zen Ai
Zen Ai - avatar
+ 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.
4th Sep 2022, 11:10 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 3
Korkunç el Gato continues trials and errors and corrections of those errors leads to mastery✨✊
5th Sep 2022, 5:42 AM
Zen Ai
Zen Ai - avatar
+ 2
Slick Thanks a lot. Cure to my confusion.
4th Sep 2022, 10:51 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 2
*y still saves the arguments in tuple format
5th Sep 2022, 11:09 AM
Ramtin Jafari
Ramtin Jafari - avatar