Data Structures

Aditya JaiswalAditya Jaiswal
5 min read

What are Data Structures?

A data structure is a way of storing data into the memory. We structure data in a different way depending on what kind of data what we have.

Why does Data Structure Matter?

In Data Science, Data Structure provide efficient ways to manage, store and retrieve the large volumes of data from the memory. It enhances data processing and analysis performances. With the help of correct data structures data scientists can process large datasets faster and use memory more efficiently. It is crucial for data analysis, machine learning and real time data processing.

Characteristics of data structures:

  1. Enable faster access and modification of data, while working with huge datasets which is common in data science.

  2. With the help of data structures algorithms run efficiently by optimizing time and space complexity, which makes the code more feasible and cost effective.

  3. Supports complex data relationships like Decision Trees and graphs in machine learning.

  4. Supports the development of scalable software systems in AI, machine learning and data driven applications by providing foundational storage and retrieval mechanisms.

Mastering Python Lists: From Basics to Common Operations

What are Lists in Python?

List is a built in datatype in Python use to store an ordered collection of Items. Items stored in the same list can be of different data types. Items of List stored in square brackets separated by commas.

List = [1, "ABC", True , 4.5]

List support indexing, slicing, iteration and other built in methods for different operations like adding , removing and transforming the elements.

Characteristics of a List in Python

  1. Elements of list are store in the same form in which the user insert them.
List = [1, "ABC", True , 4.5]
# They will be stored in the same way as they are inserted.
  1. List are mutable data types it means we can change the elements of the list in place after the creation.
List = [1, "ABC", True , 4.5]
List[0] = 100
print(List)
# [100, "ABC", True , 4.5]
  1. List allow Duplicates it means we can enter the same elements in the same list multiple times.
List = [1, "ABC", True , True]
print(List)
# [1, "ABC", True , True]

Accessing List Items

Accessing the items of list is a common operation of list and can be done through different techniques. The techniques are as follows:

  1. The most simple way of accessing item in a list is using the elements idex in every list indexing starts from 0 for the first element and for the last element it starts from -1.
a = [10, 20, 30, 40, 50]

# Accessing the first item
print(a[0])  

# Accessing the last item
print(a[-1])

Output:

10
50
  1. We can access the elements of current list and create a new list using the list comprehension.
a = [10, 20, 30, 40, 50]

# Create a new list with items greater than 20
b = [i for i in a if i > 20]
print(b)
[30,40,50]
  1. Slicing is another way of accessing items in the list. We can get a range of items specifying the starting and the ending index.
a = [10, 20, 30, 40, 50]
print(a[1:4])
20,30,40
  1. We can use for loop to access all items of the list at once.
a = [10, 20, 30, 40, 50]
for i in range a:
10
20
30
40
50

Common Operations on Lists in Data Structures

Lists are one of the most versatile and frequently used data structures. They store elements in a linear order and allow various operations for adding, removing, updating, and accessing data.

1. Creation of a List

A list can be created using square brackets [] or the list() constructor.

pythonCopyEditmy_list = [1, 2, 3, 4]
empty_list = list()

2. Accessing Elements

Elements are accessed using indexing (0-based).

pythonCopyEditprint(my_list[0])   # First element
print(my_list[-1])  # Last element

3. Traversing a List

You can loop through elements using for loops.

pythonCopyEditfor item in my_list:
    print(item)

4. Insertion

  • append() – Adds an element to the end.

  • insert(index, value) – Adds an element at a specific position.

  • extend(iterable) – Adds multiple elements at once.

pythonCopyEditmy_list.append(5)
my_list.insert(2, 10)
my_list.extend([6, 7])

5. Deletion

  • remove(value) – Removes the first occurrence of a value.

  • pop(index) – Removes and returns an element at an index (default: last element).

  • del – Deletes an element or the whole list.

  • clear() – Empties the list.

pythonCopyEditmy_list.remove(10)
my_list.pop(0)
del my_list[1]
my_list.clear()

6. Searching

  • in keyword – Checks if an element exists.

  • index(value) – Returns the first index of the value.

pythonCopyEditprint(3 in my_list)  # True or False
print(my_list.index(3))

7. Updating

Lists are mutable, so you can change elements directly.

pythonCopyEditmy_list[0] = 100

8. Sorting

  • sort() – Sorts the list in place.

  • sorted() – Returns a sorted copy.

pythonCopyEditmy_list.sort()
sorted_list = sorted(my_list, reverse=True)

9. Slicing

Retrieve a subset of elements using list [start:end:step].

pythonCopyEditprint(my_list[1:4])   # Elements from index 1 to 3
print(my_list[::-1])  # Reversed list

10. Aggregation Functions

Common built-in functions:

pythonCopyEditprint(len(my_list))  # Number of elements
print(sum(my_list))  # Sum of elements
print(min(my_list))  # Smallest element
print(max(my_list))  # Largest element
1
Subscribe to my newsletter

Read articles from Aditya Jaiswal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Jaiswal
Aditya Jaiswal