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, ...

Get Python: Data Analytics and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.