DEV Community

Cover image for Ordered Vs Unordered In Python
Oshi Gupta
Oshi Gupta

Posted on

Ordered Vs Unordered In Python

There are different types of data structures in Python such as lists, tuples, dictionaries, and more.

Lists

It is a collection of a mutable ordered sequence of elements and can be accessed through indexing and defined inside []. Each element inside the list is called an item.

List Example

From the above image, it is clearly seen that list remembers the order of insertion or follows a sequence in which elements are inserted.

Tuples

It is a collection of ordered and immutable elements. Tuples are sequences, just like lists. It is defined inside () and can be accessed through indexing.

Alt Text

The above image shows that the tuple is ordered data structure and follows the insertion sequence.

Dictionary

It is a collection in which items are defined in key-value pairs inside {}. These items are unordered, changeable, and value is accessed through the particular key.
Each key is separated from its value by a colon (:), the items are separated by commas.
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.

Alt Text

In the above image, the order of insertion and output of the dictionary is different but what remains the same is the mapping of values with keys as the positioning might
vary.

In the dictionary, we don't access items based on position unlike in lists and tuples but through keys.

Discussion (0)