DEV Community

Cover image for Introduction to data structures and algorithms with python
James-Karanja
James-Karanja

Posted on

Introduction to data structures and algorithms with python

List
A list in python is a built-in data type which is used to store multiple items in a single variable. Lists are created using square brackets.
List items are ordered, changeable and allow duplicate values. List items are indexed; the first item has index 0 the second item has index 1.

Dictionaries
Dictionaries are used to store data values in Key:Value pairs. Dictionaries are ordered, changeable and do not allow duplicates.
Dictionaries are written with curly brackets and have keys and values.
Items in a dictionary can be referred to by using the key name.

Tuples
Tuples are used to store multiple items in a single variable. A tuple is one of four built-in data types in python used to store collections of data. A tuple is a collection which is ordered and unchangeable. A tuple is written with round brackets.
Tuples are ordered and changeable and allow duplicate values. Tuple items are indexed; the first item has index 0 the second item has index 1.

Sets
A set in python is another type of built-in data type. Sets are used to store multiple items in a single variable. A set is a collection which is ordered and changeable and indexed.
NB: Set items are unchangeable but you can remove items and add new items.
Sets are written with curly brackets.

Queues
A queue is a linear data structure which means that data elements are stored in a sequential manner. A queue uses a First In First Out (FIFO) method of storage that means the last recently added item is removed first. For example; in any queue of consumers for resources, the consumer that came first is served first.

*Stack *
A stack is another linear data structure that is very similar to a queue. It however follows the Last In Last Out (LIFO) method of storage; that is, the last item that goes in is the first item that goes out. An example of stack is when plates are arranged on a shelf. Stacks can be implemented in browsers where the last opened tab will be the top most item in the history.

Linked list
A linked list is a sequence of data elements, which are connected together via links. Each data element contains a connection to another data element in the form of a pointer. They are implemented using the node concept. A node object is created and then another class is created to use this node object. Appropriate values are passed through the node object to point to the next data elements.

Latest comments (0)