0
Sequence unpacking in tuples
Could you please tell me about it with example? And why the * change a tuples into a list?
3 Answers
+ 1
Rouza, sequence unpacking in Python is basically assigning the items within a tuple to multiple variables. You are dismembering the tuple into new variables, while keeping the original intact. This is useful for handling data further.
Example:
#begin
numbers = (1, 2, 3, 4, 5)
a, *b, c = numbers
#end
In this case:
a gets the first element, the number 1.
c gets the last one, number 5.
b collects the middle elements because you used the * operator. B = [2,3,4], then, and is a list.
Here's the beauty of it: the * operator collects elements into a list because lists are more flexible. Python understands we will do something with this data (add more numbers, split them, calculate them).
0
Hello dear how are you doing đ
0
Hi