# string a, b, c = "123" # a = "1", b = "2", c ="3" # list a, b, c = [1, 2, 3] # a = 1, b = 2, c = 3 # range a, b, c = range(3) # a = 0, b = 1, c = 2 # set a, b, c = {1, 2, 3} # a = 1, b = 2, c = 3 a, b, c = {3, 2, 1} # a = 1, b = 2, c = 3 # dict dict_num = {"one": 1, "two": 2, "three": 3} a, b, c = dict_num # a = "one", b = "two", c = "three" a, b, c = dict_num.values() # a = 1, b = 2, c = 3 a, b, c = dict_num.items() # a = ("one", 1), b = ("two", 2), c = ("three", 3) # assignment to the list [a, b, c] = 1, 2, 3 # a = 1, b = 2, c = 3 [a, b, c] = "123" # a = "1", b = "2", c ="3" # Examples: my_tuple = ("Anna", 27, "Python Developer") name, age, career = my_tuple print(name, age, career) # Using starred variables # *a, *b = 1, 2, 3, 4 # Error: Only one starred expression is allowed a, *b = 1, 2, 3, 4 # a = 1, b = [2, 3, 4] print(a, b) # *a, *b, *c = 1, 2, 3 # Error: Only one starred expression is allowed # Practice # Unpacking a tuple my_tuple = (1, 2, 3) a, b, c = my_tuple # a = 1, b = 2, c = 3 print(a, b, c) # Unpacking a list my_list = [4, 5, 6] d, e, f = my_list # d = 4, e = 5, f = 6 print(d, e, f) # Unpacking a string my_string = "abc" x, y, z = my_string # x = "a", y = "b", z = "c" print(x, y, z) # Unpacking a set my_set = {7, 8, 9} a, b, c = my_set # a = 7, b = 8, c = 9 print(a, b, c) # Unpacking a dictionary values my_dict = {"name": "John", "age": 30, "city": "New York"} name, age, city = my_dict.values() print(name, age, city) # Unpacking a dictionary items my_dict = {"name": "John", "age": 30, "city": "New York"} name, age, city = my_dict.items() # name = ("name", "John"), age = ("age", 30), city = ("city", "New York") print(name, age, city) # Unpacking a dictionary vaules into a list name_list = [name[1], age[1], city[1]] print(name_list) # Unpacking a dictionary values into a list using list comprehension name_list = [name[1] for name in my_dict.items()] # Extracting names from the dictionary items print(name_list) # Unpacking a dictionary items into a list item_list = [item for item in my_dict.items()] print(item_list) my_list = list(my_dict.items()) # Converting dictionary items to a list of tuples print(my_list) my_list = list(my_dict.values()) # Converting dictionary values to a list print(my_list) # Dropping unneeded variables # Unpacking with an underscore to ignore variables my_tuple = (1, 2, 3, 4) a, b, _, d = my_tuple # a = 1, b = 2, d = 4 print(a, b, d) # Unpacking with an underscore to ignore variables in a list my_list = [5, 6, 7, 8] e, f, _, g = my_list # e = 5, f = 6, g = 8 print(e, f, g) # Unpacking with an underscore to ignore variables in a string my_string = "xyz" x, y, _ = my_string # x = "x", y = "y" print(x, y) # Unpacking with an underscore to ignore variables in a set my_set = {10, 11, 12} a, b, _ = my_set # a = 10, b = 11 print(a, b) # Unpacking with an underscore to ignore variables in a dictionary my_dict = {"key1": "value1", "key2": "value2", "key3": "value3"} key1, key2, _ = my_dict # key1 = "key1", key2 = "key2" print(key1, key2) # Unpacking with an underscore to ignore variables in a dictionary values my_dict = {"name": "Alice", "age": 25, "city": "Wonderland"} name, age, _ = my_dict.values() # name = "Alice", age = 25 print(name, age) # Unpacking with an underscore to ignore variables in a dictionary items my_dict = {"name": "Bob", "age": 30, "city": "Atlantis"} name, age, _ = my_dict.items() # name = ("name", "Bob"), age = ("age", 30) print(name, age) # Using unpacking to create a new dictionary from an existing one my_dict = {"name": "Bob", "age": 30, "city": "Atlantis"} name, age, _ = my_dict.items() # name = ("name", "Bob"), age = ("age", 30) tuple my_new_dict = {name[0]: name[1], age[0]: age[1]} # Creating a new dictionary print(my_new_dict) # Using unpacking to create a new dictionary from an existing one using dict my_dict = {"name": "Bob", "age": 30, "city": "Atlantis"} name, age, _ = my_dict.items() # name = ("name", "Bob"), age = ("age", 30) tuple my_new_dict = dict(name=name[1], age=age[1]) # Creating a new dictionary print(my_new_dict) a, *b, c = 1, 2, 3, 4, 5 # a = 1, b = [2, 3, 4], c = 5 print(a, b, c) a, _, b, *_ = 1, 2, 3, 4, 5 # a = 1, b = 2, _ = 3, *_ = [4, 5] print(f"a={a}, b={b}")