Declaring a List and unpacking a List

A list is a group of elements of different type. It is a in-built data structure in Python. Lists are ordered and elements of list can be changed (mutable). 

Below is an example of declaration of list:

sites = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve"]

List can be unpacked as below:

site_1 , site_2, site_3, site_4 = 
["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve"]

print(site_1)
print(site_2)

In the above code, list is unpacked. Unpacking meant the values in the list are assigned to the variables. The rule is that to unpack a list, variables equal to number of elements in the list must be assigned.

If the list is assigned to variables count less than elements of list, as below, then Python throws ValueError

site_1,site_2,site_3 = ["embeddeddesignblog","py-programmers","youtube-Way2know","TalentEve"]
print(site_1)
print(site_2)

ValueError: too many values to unpack 

Post a Comment

0 Comments