Menu

Working with Lists

Working with Lists

Lists are ordered collections that can store multiple values in a single variable. In data science, lists are useful for storing measurements, product prices, customer ratings, and other collections of values.

Creating and Accessing Lists

product_prices = [1200, 850, 1500, 2200, 950]
print(product_prices)
print(product_prices[0])   # First price
print(product_prices[-1])  # Last price

Python uses zero-based indexing, meaning the first element is at index 0. Negative indexing allows you to access elements from the end of a list.

Modifying Lists

Lists are mutable, so you can change their contents after creation.

product_prices = [1200, 850, 1500]
product_prices.append(1800)
product_prices[1] = 900
print(product_prices)
You can also remove elements and sort values.
product_prices.remove(1200)
product_prices.sort()
print(product_prices)

Extracting a Subset of Values

List slicing extracts a portion of a list using the syntax list[start:stop]. The stop index is excluded.

monthly_sales = [12000, 15000, 18000, 14000, 21000, 25000]

first_quarter = monthly_sales[:3]

print(first_quarter)

Lists are useful for small collections and general-purpose programming. For large numerical datasets, NumPy arrays usually provide more efficient mathematical operations.


Python for Data Science

VA

Vishalini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details