Python: Data Structures
Photo by Markus Spiske

Python: Data Structures

Consider a scenario where you are at your home looking for your socks in a pile of clothes. You can imagine how frustrating it would be if they decided to get a divorce and are now lying at the opposite ends of the pile. It would be damn difficult to find them. This is why you need a sock drawer to store them in a better way at a single place. It is the way of organizing our clothes efficiently. This is roughly what a data structure is. In programming languages, a data structure is the representation of data. If you go through the formal definition it's defined as "Mathematical or Logical representation of data". These generally optimize how a computing process manages information in memory.

These data structures, in particular, specify types of data, and thus which operations can be performed on them while eliminating the need for a programmer to keep track of memory addresses. 

In Python the four major data structures are:

Data Structures in Python

List:

Lists are one of the most versatile, useful data types in Python. A list is an ordered collection of arbitrary objects. It is somewhat similar to an array but is a bit more flexible. In Python, lists are defined by enclosing a comma-separated sequence of objects in square brackets[].

#List lst

lst = ["Anamika",1,2,3]

print(lst)

Some important points to keep in mind:

  • Lists are mutable in nature that is you can modify a list at any given time wherever the scope.
  • Lists can be accessed using the index value.
  • It can be nested to arbitrary depth.
  • Lists are dynamic. Since the list is mutable it implies items could be inserted and deleted from anywhere in the list.

Tuple:

Tuples resemble lists in many ways as they are sequences just like lists. The only difference is that the items of lists are mutable whereas tuples contain immutable elements. That implies you cannot modify a tuple once it's created. Tuples are represented using parenthesis.

# Creating an empty tuple

n_tuple =()

print(n_tuple)

# Tuple containing integers

n_tuple = (1,2,3)

print(n_tuple)


Points to remember:

  • A tuple is a preferred data type when we do not want to change the data over time.
  • It is faster to iterate over the elements of a tuple than a list. This is because Tuples are stored in a single block of memory. As they are immutable, it doesn't require extra space to store new objects. Lists are allocated in two blocks: the one that is fixed with all the Python object information and another a variable-sized block for the data.
  • Elements of a tuple are enclosed in parenthesis.

Dictionary:

The first thing that crosses our mind when we hear the word dictionary is a big book that contains the answer to every "difficult" word we ever come across. If you think about it a dictionary had words working as keys and their meanings were the values. But they were alphabetically ordered. In python, a dictionary is an unordered collection of items. The compound data types have only value as an element while a dictionary has a key: value pair.

# Creating an empty dictionary

dct ={}

#dictionary with key value pairs:

dct = {1: 'orange', 2: 'banana'}

print(dct)

# dictionary with mixed keys

my_dict = {'name': 'Jace', 1: [8, 4, 7]}

Points to remember:

  • Each key is separated from its value by a colon (:), the items are separated by commas. The whole dictionary is enclosed in curly braces.
  • Keys are always unique within a dictionary while values may not be. (Just like the real one.
  • When the key is known, dictionaries are optimized to retrieve values.


Set:

Let us refresh our memories of high school where we were first introduced to sets and our first reaction was "does this even need a revision?" You might remember the Venn diagram.

No alt text provided for this image

So mathematically speaking, a set is a well-defined collection of distinct objects. It can be beneficial in programming as well to group the objects into a set. Python provides a built-in set type to do so. In Python, Set is a collection of unordered and unindexed items. Sets are written with curly brackets.

# creating a set: 
s_set = {"apple", "banana", "cherry"}
print(s_set)


Points to remember:

  • Sets are un-ordered and unindexed
  • All the elements in a set are unique. Generally speaking, duplicate elements are not allowed.
  • The elements contained in the set must be of an immutable type while a set itself may be modified.
Rohit S ModGil

Software Developer | Angular - Node.js - SQL

5 年

Wow! Well elaborated!

Aditya Verma

Quantexa Certified Data Engineer | Scoring Engineer | Data Science and Analytics | Crypto Enthusiast | Scala | Spark | ETL

5 年

I guess we all need to learn about data structures to save our relationships. Thanks for the insight.

要查看或添加评论,请登录

Ankisha Sharma的更多文章

  • ANN: Components

    ANN: Components

    Neurons, the reason our brain works. Neurons are the sole reasons for our reflexes, our activities (besides our will…

    1 条评论
  • Gradient Descent: An Introduction

    Gradient Descent: An Introduction

    Gradient descent is an optimization method that helps us find the precise combination of weights for a network that may…

    2 条评论
  • Introduction to Pandas

    Introduction to Pandas

    Python was the third most used programming language in 2019. Being the third most used qualifies it to be a useful…

    1 条评论
  • RSA Cryptosystem

    RSA Cryptosystem

    You can not tell me that you studied cryptography and not once this algorithm troubled you. Students always are…

    3 条评论
  • Python: Modules

    Python: Modules

    According to the formal definition, a module stands for, one of a set of separate parts that, when combined, form a…

    3 条评论
  • Python: A brief history

    Python: A brief history

    Gone are the days when python was the name of a non-venomous snake. Nowadays this one big snake is biting a lot of…

    2 条评论

社区洞察

其他会员也浏览了