Other data structures
Python has data structures such as stacks, lists, sets, sequences, tuples, lists, heaps, arrays, dictionaries, and deque. We have already discussed lists while attempting to understand arrays. Tuples are typically more memory efficient than lists because they are immutable.
Stacks
The list
method is very convenient to be used as a stack, which is known to be an abstract data type with the principle of operation last-in, first-out. The known operations include adding of an item at the top of the stack using append()
, extracting of the item from the top of the stack using pop()
, and removing of the item using remove(item-value)
, as shown in the following code:
stack = [5, 6, 8] stack.append(6) stack.append(8) stack [5, 6, 8, 6, 8] stack.remove(8) stack [5, 6, 6, 8] stack.pop() 8 stack.remove(8) Traceback (most recent call last): File "<ipython-input-339-61d6322e3bb8>", line 1, in <module> stack.remove(8) ValueError: list.remove(x): x not in list
The...