Skip to main content

Built in data structures in Python

This post only contains an introduction to the built in data structures within python.The rules for defining and the methodology for using it will be defined in other posts.

Before moving forward with the details we need to understand what data structure is and why it is important in programming.

Formal definition of a data structure:

"In computer science, a data structure is a data organization, management and storage format that enables efficient access and modification.More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data."[5]

Now we know what data structure is.Next, we will see its importance in programming.

Why data structure is important in programming?
 A solution to a problem in programming involves taking an input and providing the solution of the output. The intermediate process where the problem resolution is done involves the use of algorithms and data structure. Efficient use of the memory to store and retrieve the data is realized with the help of data structure.

If we compare with real world, we do employ similar strategies like using stack to store the dishes in the shelves, stacks of goods in warehouses, list of items or task in to do lists, queue of people at a bank or a bus stop.

Data structure are every where, but today we going going to limit our discussion to the built-in data structure within python only.


List of built-in data structures within python are:
1.List
2.Dictionary
3.Sets
4.Tuples
5.String
6.Frozensets

Lets see the definition of each data structure.

1.List

Definition:

"Lists are mutable ordered and indexed collections of objects. The items of a list are arbitrary Python objects. Lists are formed by placing a comma-separated list of expressions in square brackets. (Note that there are no special cases needed to form lists of length 0 or 1.)"[3]

Example of list in python:


In the snippet above it can be seen that the lists in python are created using square brackets. The list can contain different types of objects in one list too.

Next, we have

2.Dictionary

Definition:
"A dictionary is a sequence of items. Each item is a pair made of a key and a value. Dictionaries are not sorted. You can access to the list of keys or values independently."[3]

Example of dictionary in python:


From the code snippet it can be seen that the dictionary is a key value pair and is declared in python using curly brackets. Also keep in mind that the keys in python should be unique otherwise there will be problems while inserting duplicate keys.

3.Sets

Definition:

" A set is an unordered collection with no duplicate elements."[3]

Example of Sets in python:


Curly braces or the set() function can be used to create sets. The set contains no duplicate entries which is much evident in the code snippet when we declare the set using set function. The word alacazam contains repeating alphabets but when the set is printed it contains only unique alphabets.

4.Tuple

Definition:

"Tuples are immutable sequences, typically used to store collections of heterogeneous data (such as the string and integer together). Tuples are also used for cases where an immutable sequence of homogeneous data is needed (such as allowing storage in a set or dictionary instance). "[3]

Example of tuple in python:



Tuples in python can be declared in python using round brackets. The objects inside tuples are immutable. If you try to change the objects inside the tuple you will get error during the process.

5.String

Definition:
"A string is a collection of characters enclosed within quotes"[3]

Example of String in python:



The above given code snippet contains a string which contains a sequence of characters enclosed within quotes,which can be single or double.


6.Frozenset

Definition:
Sets are mutable, and may therefore not be used, for example, as keys in dictionaries.
Another problem is that sets themselves may only contain immutable (hashable) values, and thus may not contain other sets.
Because sets of sets often occur in practice, there is the frozenset type, which represents immutable (and, therefore, hashable) sets.[2][3]

Example of frozensets:


Frozenset helps to create a set inside set. Also the dictionary can contain these frozenset as a key.

For more details refer the reference. 

That's all for this post!!

Thank you for reading this post.
If you have any suggestions regarding the post contents or if you need some more details on any other topic, please post it in the comments section.
Your suggestions are too valuable so they should not be missed.


References:
1. https://www.quora.com/Why-are-data-structures-and-algorithms-so-important-in-computer-science
2. https://docs.python.org/3/tutorial/datastructures.html
3. https://python-reference.readthedocs.io/en/latest/docs/list/
4. https://www.tutorialspoint.com/execute_python_online.php
5. https://en.wikipedia.org/wiki/Data_structure

Comments

Popular posts from this blog

PyMuPDF vs PDFMiner

 As a developer , I was tasked to extract specific data from a PDF. Upon analysing it further, certain patterns were found based on keywords in the document. Since I was using Python language for the task I found 2 tools quite useful which are PyMuPDF and PDFMiner. These tools can then be used to extract the text from a page on which regular expression can be applied to further extract relevant data.     Next, we are going to take a deeper look into these tools, specifically focusing on the pros and cons of each.     PyMuPDF   Docs , PIP package Pros Simple and understandable API Extensive tools to work with text, images, and graphics Available as a PIP package (pip install PyMuPDF) Better support for a range of symbols comparer to PyPDF2   Cons Parsed text is not in sequence Dependency on other package-Fitz Text sequence information lost during extraction     PDFMiner   Docs ,  PIP package ...

Finding difference between 2 files in Python

In this post, we will take a look at how to compare two files using Python.   I was tasked to compare 2 files and then list the differences between them using Python. Initially, I started with filecmp module, but even with the function parameter ‘ shallow’ set to false, the Boolean result was not enough. Sure, it can act as an indicator to take some action, but it will not list the differences.   I was looking for something more visual, something like color coding and not like the git diff output, which is not very user-friendly. But, another Python internal module, difflib helped me to get the job done.   Inside Difflib, HtmlDiff is what I was looking for. The differences were highlighted with 3 different colors and also the line numbers were indicated in a table to locate the differences. The results are quite self-explanatory and it is easier to explain the differences to other people. Code for generating the above difference table: Note: File1...

Adding existing Anaconda environment to Jupyter notebook

In this post we are going to take a look at adding Anaconda environment to Jupyter notebook. Recently, I was working on a CSV file and wanted to work with Pandas package for tabular data manipulation using Python. The problem was even if I install Pandas package, I would have to install other Data Science package as needed. But, the Anaconda environment was already setup on my laptop, which I want to reuse.   Today, we will look into how to reuse the Anaconda environment within the Jupyter Notebook.   There are 4 basic steps to be followed for adding the environment: 1. Create a conda environment Go to Conda command prompt(Run in Admin mode) Run the following command: conda create –-name newenv O/P:   What if there is an existing conda environment? Go to Conda command prompt(No need for Admin mode) Run the following command: conda env list O/P: Since there was only one environment, only one entry was displayed. ‘*’ indicates the cur...