Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

List assignment:

[a,b]=[1,2]

Tuple assignment:

a,b=[1,2]

I don't perceive any distinction in the consequence of these two sorts of tasks, is there any distinction I don't have a clue yet? 

In the event that there isn't, the reason would they say they are called various names?

closed

4 Answers

0 votes
by (25.7k points)
selected by
 
Best answer
In Python, both list assignment and tuple assignment may seem similar when assigning values to variables. However, there is an important distinction between the two.

List assignment [a, b] = [1, 2] assigns the values 1 and 2 to variables a and b, respectively, creating a list on the right side and unpacking its values into variables on the left side. In this case, the result is that a will be assigned the value 1, and b will be assigned the value 2.

Tuple assignment a, b = [1, 2] also assigns the values 1 and 2 to variables a and b, respectively. However, the right side creates a tuple, and the values are unpacked into variables on the left side. Again, a will be assigned the value 1, and b will be assigned the value 2.

The distinction lies in the type of object created during the assignment. List assignment creates a list on the right side, while tuple assignment creates a tuple. This difference becomes more apparent when assigning multiple values or when dealing with more complex data structures.

Although the result may appear similar in simple cases like the one you provided, the distinction in data type is important and can affect how the assigned values are treated and manipulated in subsequent code.

Therefore, while the initial outcome may be the same, understanding the difference between list assignment and tuple assignment becomes significant when working with more advanced programming scenarios and data structures.
0 votes
by (26.4k points)

They do exactly the same thing. A tuple is a perused just form of a list. As a rule, you use enclosures/parentheses (a, b) to make tuples versus square sections [a, b] for records/lists, however, the brackets can in some cases be overlooked. You could likewise compose:

(a,b) = [1,2]

Or, perhaps most common:

a,b = 1,2

Interested to learn the concepts of Python in detail? Come and join the python course to gain more knowledge in Python

Watch this video tutorial, if you want to gain more information

0 votes
by (15.4k points)
In Python, list assignment and tuple assignment may appear similar when assigning values to variables. However, there is a key distinction between the two.

List assignment, such as [a, b] = [1, 2], creates a list on the right side and unpacks its values into variables on the left side. This means that a will be assigned the value 1 and b will be assigned the value 2.

Tuple assignment, like a, b = [1, 2], also assigns values to variables a and b. However, it creates a tuple on the right side and unpacks the values accordingly. Once again, a will be assigned the value 1 and b will be assigned the value 2.

The distinction lies in the type of object created during the assignment. List assignment generates a list, while tuple assignment generates a tuple. This difference becomes more significant when dealing with multiple values or complex data structures.

Although the outcome may seem identical in simple cases, understanding the difference in data type becomes important when working with more advanced programming scenarios and data structures.
0 votes
by (19k points)
List assignment [a, b] = [1, 2] and tuple assignment a, b = [1, 2] may seem similar, but they differ in the data type created during the assignment. List assignment creates a list, while tuple assignment creates a tuple. This distinction becomes important when working with more complex scenarios and data structures.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Sep 17, 2019 in Python by Sammy (47.6k points)

Browse Categories

...