Data Structures

Data Science with Python: Basic Data Structures

In the previous article, we dive into the details of Data Types in Python. Now, let’s move to basic data structures in Python. Data Structures are building blocks for a programming language. To write programs we should have a good understanding of data structures.

What is Data Structure?

When doing data operations, data is organized, processed, and stored using data structures to maximize its usefulness. There are many basic and advanced types of data structures, each has its processing technique and purpose. You can learn basic data structures in Python easily from this tutorial.

Data Structures in Python

There are five basic data structures in Python.

  1. List
  2. Tuple
  3. Set
  4. Dictionary
  5. String

Let’s discuss all data structures one by one in detail:


The list is the most versatile data structure in Python. In the list, we have comma-separated values written in the square bracket.

Properties of Python lists are:

  1. Lists are mutable and individual elements of a list can be changed.
  2. The list can have values of different types, but preferably keep items of the same type.
Python Code:

print(“List: “,list1)
print(“The second element of the list is: “,list1[1])


>>>List:  [10, 4.5, ‘test’]

>>>The second element of the list is:  4.5

See the above example code, we list different types of elements and print its second element. Please notice an index of the list starts from 0, not 1.

Let’s change the second element of the list and print the list. The element will change because lists are mutable.

#Lists are mutable, which means we can alter any element value in the list

print(“New list: “,list1)


>>>New list:  [10, 3, ‘test’]


Tuples are represented by the number of values separated by commas written in the round bracket.

Properties of Python Tuples are:

  1. Python tuples are immutable.
  2. Tuples are a secure option for storing data where data need to remain constant throughout the execution of a program.
Python Code:

tup1=(1,3.6, “hello”)
print(“Tuple: “,tup1)
print(“Tuple: “,tup1)


>>>Tuple:  (1, 3.6, ‘hello’)

>>>TypeError: ‘tuple’ object does not support item assignment #Tuples are immutable

In the above sample code, when we try to change the second element of the tuple, it throws an error. Thus, we can say tuples are immutable.


A Set is a collection of unordered elements. In Python, sets are written within curly brackets.

Properties of Set are:

  1. Every set element is unique (no duplicates).
  2. Sets are immutable (cannot be changed). However, we can add or remove items from it.
Python Code:

print(“Set: “,myset)



>>>Set: {10, 20, 30}         #Print unique items only

>>>TypeError: ‘set’ object does not support indexing     #Set does not support indexing as there is no order

From the above example, the set only contains unique elements and does not support Indexing.

Python Code:

print(“New Set: “,myset)


>>>New Set: {10, 20, 90, 30}  #New item added and there is no order in the set.

In the above example, we can see that a new item was added to the set and there is no order in the set.


Python Dictionary is an unordered collection of items. Dictionaries are created with key-value pairs separated by colons using curly brackets.

Properties of Dictionaries are:

  1. Dictionaries are optimized to get the values from known keys.
  2. Dictionaries offer an incredibly effective way to store data, with an O(1) time complexity when retrieving items.
Python Code:

#Python Dictionary is an unordered collection of key-value pairs




As shown in the above example, values of keys can be fetched easily from the dictionary.


In Python, strings can be created by enclosing characters inside a single quote or double quotes. Strings are an array of bytes representing Unicode characters.

Properties of Strings:

  1. The strings are case-sensitive.
  2. Strings are immutable.
Python Code:

str =”Data Science”
print(str)    #Print String

print(str[0])    #Print first character of a string
print(str[-1])    #Print the last character of a string
print(str[5:])    #String slicing
print(len(str))    #Print length of string


>>>Data Science

The above code example of strings shows the various operations on Strings.


In this article, we have explained various basic data structures in Python in brief with real examples. They are building blocks of learning Python and are very important to understand for advanced topics.

Stay Tuned!!

Data Science with Python: Basic Data Types

Keep learning and keep implementing!!

3 thoughts on “Data Science with Python: Basic Data Structures”

  1. The topic is explained so nicely by using very simple language that as a beginner I am able to clearly understand and getting the confidence that I can learn Python easily.

Leave a Comment

Your email address will not be published. Required fields are marked *