Container Data Structures
Lists in Detail
Lists and tuples can contain items of mixed types and the type of any item can be retrieved by using the type()
function:
list_of_mixed_items = ['red', 2.3, 4, True,
["another", "list"],
("a" , "tuple"),
{"a" , "set"},
{"and": 1, "a": 2, "dictionary": 3}]
for item in list_of_mixed_items:
print(type(item))
Output:
<class 'str'>
<class 'float'>
<class 'int'>
<class 'bool'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
When constructing a container data structure such as a list, tuple, set, or dictionary with variables of primitive types, namely Integers, Floats, Strings, and Booleans, the values of those variables are copied into the container data structure. If those variables are reassigned with other primitive values after the populating the container, the values in the list are preserved (referencing the value).
If the variable placed in a container is a complex data type such as a custom object, list, set, turtle, etc. then the container keeps a reference to the address of that variable in the memory of the computer, i.e. always referring to the present value of the variable even if its contents are modified (referencing the variable). Even though the tuples themselves cannot be modified, the values of the complex variables stored in that tuple can be modified, and that is not a violation of the definition of the tuple, which may hold any data type including primitive and complex types.
If a complex variable placed in a container is reassigned, then the container (e.g. list) keeps referring to the previous address of that variable. We can think of this as both the list and the complex typed variable are pointing to the same location in the memory at the same time. Even if one of them stops referring to that address, the other can keep referring as long as required.
If there is no reference left to a specific value in the memory, then the interpreter will utilize a garbage collection algorithm to reclaim the space consumed by un-reachable data. Otherwise, the application may run out of memory due to these memory leaks.
color = "yellow"
number = 10
boolean = True
sublist = [123, 213]
tuple_of_mixed_items = (color, number, boolean, sublist)
color = "red"
number = 11
boolean = False
sublist[0] = 34
sublist.append(54)
for item in tuple_of_mixed_items:
print(item)
sublist = [36, 67]
print(tuple_of_mixed_items)
mylist=[color,number]
print(mylist)
color = "blue"
print(mylist)
Output:
yellow
10
True
[34, 213, 54]
('yellow', 10, True, [34, 213, 54])
Elements in a tuple can be assigned (unpacked) to multiple variables at once, possibly as a list when using an asterisk (*
) to contain all items left without a variable:
person = (23, "Ghida", "Student", 20009001, "Reading", "Maths", "Address", "ghida@some.mail")
age, name, occupation, id, *other_info, email = person
print(age)
print(name)
print(occupation)
print(id)
print(other_info)
print(email)
Output:
23
Ghida
Student
20009001
['Reading', 'Maths', 'Adress']
ghida@some.mail