Container Data Structures

Operations

Assigning an existing list variable to another variable will result in both variables to be pointing to the same list instance:

colors = ["black", "azure", "ruby", "orange", "blue", "violet", "pink" ]
colors_temp = colors

colors.sort()

print(colors_temp)

Output:['azure', 'black', 'blue', 'orange', 'pink', 'ruby', 'violet']


If a copy of the original list is required, the copy() function can be used for that purpose to generate another instance of the list so that any modification to one list will not affect the other:

colors = ["black", "azure", "ruby", "orange", "blue", "violet", "pink" ]
colors_temp = colors.copy()
colors.sort()

print(colors)
print(colors_temp)

Output:

['azure', 'black', 'blue', 'orange', 'pink', 'ruby', 'violet']
['black', 'azure', 'ruby', 'orange', 'blue', 'violet', 'pink']

New lists can be generated using other lists, sets, or tuples with the list() constructor method, but applying list() constructor with a dictionary as the argument will ignore the value part of the paired items in the dictionary, populating the list only with the key set:

colors = ["black", "azure", "ruby", "orange", "blue", "violet", "pink" ]
colors_temp = list(colors)
colors.sort()
print(colors)
print(colors_temp)

colors_tuple = ("red", "purple", "cyan")
print(colors_tuple)
colors_temp = list(colors_tuple)
print(colors_temp)

colors_set = {"yellow", "white", "magenta"}
print(colors_set)
colors_temp = list(colors_set)
print(colors_temp)

colors_dict = {"yellow": 1, "blue": 2}
colors_temp = list(colors_dict)
print(colors_temp)

Output:

['azure', 'black', 'blue', 'orange', 'pink', 'ruby', 'violet']
['black', 'azure', 'ruby', 'orange', 'blue', 'violet', 'pink']
('red', 'purple', 'cyan')
['red', 'purple', 'cyan']
{'magenta', 'yellow', 'white'}
['magenta', 'yellow', 'white']
['yellow', 'blue']

New tuples can be generated from existing tuples and lists by using tuple() constructor:

tuple_1 = (1, 3, 5)
tuple_2 = (6, 7)
tuple_3 = tuple_1 + tuple_2
print(tuple_3)

list_1 = [10, 11, 12]
list_2 = [13, 14, 15]

tuple_4_is_a_list = list_1 + list_2
print(tuple_4_is_a_list)

tuple_5 = tuple(list_1 + list_2)
print(tuple_5)

Output:

(1, 3, 5, 6, 7)
[10, 11, 12, 13, 14, 15]
(10, 11, 12, 13, 14, 15)

Tuples and lists can be replicated as many time as required using the star * operator:

tuple_1 = (1, 3, 5)
tuple_2 = tuple_1 * 3
print(tuple_2)

Output:

(1, 3, 5, 1, 3, 5, 1, 3, 5)

The count() method returns the number of elements present in a list or a tuple having the given argument value of the count() method.

tuple_1 = (11, 13, 15, 16, 11, 14)

print(tuple_1.count(11))
print(tuple_1.count(13))
print(tuple_1.count(15))

Output:

2
1
1

Similarly, the index() method returns the index of the first item in the list or tuple having the argument value of the index()  method. Try the usage of these methods, find out what happens if the argument of the index() method does not exist in the list or the tuple, and how to detect and handle the problem in such a case.