Member-only story

19 Data Structures in Python

btd
10 min readNov 11, 2023

--

Photo by Arthur Mazi on Unsplash

Data science involves manipulating and analyzing data efficiently, and various data structures serve different purposes in this field. Here’s a list of common data structures used in data science:

1. Lists:

  • A collection of elements where each element has an assigned index.
  • Lists in Python are mutable, meaning you can modify them in-place, and they can contain elements of different data types.
  • They are commonly used for storing and manipulating sequences of items.
# Creating a list
my_list = [1, 2, 3, 'four', 5]

# Accessing elements
first_element = my_list[0] # Output: 1
last_element = my_list[-1] # Output: 5

# Slicing
subset = my_list[1:4] # Output: [2, 3, 'four']

# Appending elements
my_list.append(6)

# Removing elements
my_list.remove('four')

# Iterating through a list
for item in my_list:
print(item)

# List comprehension
squared_numbers = [x**2 for x in my_list]
  • my_list is a Python list containing a mix of integers and strings.
  • Various operations such as indexing, slicing, appending, and removing elements are demonstrated.
  • The list comprehension technique is also used to create a new list of squared numbers based on the original list.

2. Arrays:

--

--

btd
btd

No responses yet