Creating a list
Let's see how we can create an empty list:
<Variable name > = [] List1 = []
Creating a list with values
A list contains comma-separated values. For example:
Avengers = ['hulk', 'iron-man', 'Captain', 'Thor']
Unpacking list values
You can assign a list value to variables by using the assignment operator. Let's discuss this with an example:
>>> a,b = [1,2] >>> a 1 >>> b 2 >>>
You can see that 1
is assigned to variable a
and 2
is assigned to variable b
. This is called unpacking. What happens when a list is provided with more variables? Let's see the following example:
>>> a,b = [1,2,3] Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> a,b = [1,2,3] ValueError: too many values to unpack
The error indicates that there are more values to unpack. Let's see another example:
>>> a,b,c = [1,2] Traceback (most recent call last): File "<pyshell#8>", line 1, in <module>...