PYTHON DATA STRUCTURES (LISTS, SETS, TUPLES, DICTIONARIES)
Tags: webdev, python
Published: 2024-04-06 22:34:27

## INTRODUCTION

Depending on the circumstance, data can be organized using data structures to facilitate more efficient access. Any programming language's foundational concepts, or data structures, are what a program is constructed upon. Compared to other programming languages, Python makes it easier to master the fundamentals of these data structures.

We'll talk about Python data structures in this article, along with their relationships to particular Python data types. We will go over all of the built-in data structures, including dictionaries and list tuples.

## LISTS

Python lists are an ordered collection of data, just like arrays, which are stated in other languages. Since items in a list do not have to be of the same type, it is immensely flexible.

Python lists are implemented similarly to Java's ArrayList and C++'s Vectors. Since every piece needs to be moved, the most expensive action is adding or removing the element from the beginning of the list. When the preallocated memory runs out, insertions and deletions at the end of the list may also become expensive.

As demonstrated below, we can make a list in Python.

📌Example:

```
pylist= [1, 2,  3, "apple", 2.3]
print(pylist)
```

📌 The allocated index can be used to access list elements. Python lists have a starting index of 0 and an ending index of N-1, if there are N elements in the list.


## DICTIONARY

Python dictionaries have an O(1) time complexity, just like hash tables in other programming languages. A dictionary, as opposed to other data types that only include a single value as an element, contains a key:value pair. It is an unordered collection of data values that is used to store data values similar to a map. To improve optimization, the dictionary provides key-value. 

With the use of keys, the Python Dictionary is indexable. These can be of any hashable type, meaning they are objects such as strings, numbers, tuples, etc. that are immutable. Curly braces (\}) or dictionary comprehension can be used to construct a dictionary.

📌Example:

```
Dict = {'Name': 'John Doe', 1: [1, 2, 3, 4]}
print("Creating Dictionary: ")
print(Dict)
 
```

## TUPLE

Similar to a list, a Python tuple is a collection of Python objects; however, once a tuple is generated, its elements cannot be added or withdrawn. A Tuple can have different kinds of elements, much like a List.


Tuples in Python are constructed by grouping a sequence of values together using parentheses or not, and then separating the values with a "comma."

Note: Although it is a little more difficult, tuples can also be made with just one element. To make something a tuple, there needs to be a trailing "comma" in addition to the one element enclosed in parentheses.

📌Example:

```
Tuple = ('apple', 'orange')
print("\nTuple with the use of String: ")
print(Tuple)
     
```

## SET

Python Sets are changeable, unordered data collections that forbid duplicate elements. In essence, sets are utilized for duplicate entry removal and membership checking. This uses a data structure called hashing, which is widely used to execute insertion, deletion, and traversal operations in O(1) on average. 

A linked list is created by appending a value to an index point if there are several entries present at that index location. A dictionary containing dummy variables is used to implement CPython sets, with the members set having the highest optimizations for time complexity being the key.

📌Example:

```
Set = set([1, 2, 'Apple', 4, 'orange', 6,]) 
print("\nSet with the use of Mixed Values") 
print(Set) 
```