Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap the values of two variables using the tuple unpacking style

Tags:

dart

In Python, you can swap the values of two variables using the following syntax

a, b = b, a

How to do this in Dart?

like image 352
ThePiercingPrince Avatar asked Jan 04 '14 12:01

ThePiercingPrince


People also ask

What is tuple unpacking?

Python tuples are immutable means that they can not be modified in whole program. Packing and Unpacking a Tuple: In Python, there is a very powerful tuple assignment feature that assigns the right-hand side of values into the left-hand side. In another way, it is called unpacking of a tuple of values into a variable.

How does tuple unpacking work in Python?

Unpacking in Python refers to an operation that consists of assigning an iterable of values to a tuple (or list ) of variables in a single assignment statement. As a complement, the term packing can be used when we collect several values in a single variable using the iterable unpacking operator, * .


1 Answers

Python style tuple unpacking is not supported in Dart. Neither is the assignment of multiple variables as you have in your example. If it is the swap you are after, you could always just do the following:

var a = 10, b = 5, temp;
temp = a;
a = b;
b = temp;
like image 136
Shailen Tuli Avatar answered Oct 03 '22 01:10

Shailen Tuli