(Original version copied from https://github.com/blutterfly/python/blob/main/docs/examples/data_structures.md on 2025-10-21 - thank you "Butterfly", then edited over time...)
Introduction of (a few) python data structures:
Lists are used to store multiple items in a single variable.
List items can be of any data type. And a list can contain different data types.
Lists are ordered. They are changeable. They allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
When you add new items to a list, they are placed at the end of the list.
Lists are created using square brackets.
Creating a List
fruits = ["apple", "banana", "cherry"]
print(fruits)or use the list() constructor
fruits = list(("apple", "banana", "cherry"))
print(fruits)Accessing Items
print(fruits[0]) # First item
print(fruits[-1]) # Last itemModifying Lists
fruits.append("orange") # Add an item
fruits[1] = "blueberry" # Change an item
print(fruits)Iterating Through a List
for fruit in fruits:
print(fruit)Common List Methods
append(item): Add an itemremove(item): Remove an itemlen(list): Get the number of itemssort(): Sort the list
Exercise:
- Create a list of your favorite hobbies.
- Add a new hobby to the list.
- Print each hobby using a loop.
- Python List Cheat Sheet. https://www.cse.msu.edu/.../listAndTuplesCheatSheet.pdf
- Python Lists. w3schools.com/python/python_lists.asp
Dictionaries store data in key-value pairs.
A dictionary is a collection. As of Python version 3.7 dictionary items are ordered (its items have a defined order, and that order will not change). It is changeable (we can change, add or remove items after the dictionary has been created). And it does not permit duplicates (it cannot have two items with the same key).
Values in dictionary items can be of any data type.
Dictionaries are written with curly brackets, and have keys and values.
Creating a Dictionary
student = {"name": "Alex", "age": 16, "grade": "A"}
print(student)Accessing Items
print(student["name"]) # Access value by keyAdding/Updating Keys
student["school"] = "High School" # Add a new key
student["grade"] = "A+" # Update value
print(student)Iterating Through a Dictionary
for key, value in student.items():
print(key, ":", value)Common Dictionary Methods
-
keys(): Get all keys -
values(): Get all values -
items(): Get all key-value pairsExercise:
- Create a dictionary with details about your favorite book (title, author, year).
- Add a new key for the genre.
- Print all the keys and values.
- Dictionaries Cheat Sheet. https://www.cse.msu.edu/.../cheatSheet.pdf
- Python Dictionaries. w3schools.com/python/python_dictionaries.asp
Sets are used to store multiple items in a single variable.
Sets are unordered.
Set items are unchangeable and are not indexed -- but you can remove items and add new items.
A set cannot contain duplicate members.
Set items can be of any data type and a given set can contain different data types.
Sets are written with curly brackets.
Creating a Set
fruits = {"apple", "banana", "cherry"}
print(fruits)or we can use the set() constructor
fruits = set(("apple", "banana", "cherry"))
print(fruits)Because there is no index, we cannot access items in a set via an index or a key.
That said, we can loop through the set items via a for loop.
We can also see if a specified value is exists in a set via the "in" keyword.
fruits = {"apple", "banana", "cherry"}
for x in fruits:
print(x)
print("apple" not in thisset) # returns False- Dictionaries Cheat Sheet. https://www.cse.msu.edu/.../cheatSheet.pdf
- Python Sets. w3schools.com/python/python_sets.asp
...Start at: https://www.w3schools.com/python/python_sets_access.asp
What is a DataFrame?
A DataFrame is a 2-dimensional table-like data structure in the pandas library. Think of it as a spreadsheet.
Setting Up pandas
Make sure you have pandas installed:
pip install pandasCreating a DataFrame
import pandas as pd
data = {
"Name": ["Alice", "Bob", "Charlie"],
"Age": [16, 17, 16],
"Grade": ["A", "B", "A"]
}
df = pd.DataFrame(data)
print(df)Accessing Columns
print(df["Name"]) # Access a single column
print(df[["Name", "Age"]]) # Access multiple columnsFiltering Rows
print(df[df["Age"] > 16]) # Students older than 16Adding a New Column
df["Passed"] = [True, False, True]
print(df)Iterating Through Rows
for index, row in df.iterrows():
print(row["Name"], "is", row["Age"], "years old.")Exercise:
- Create a DataFrame with data about your favorite movies (columns: Title, Year, Genre).
- Add a new column for Rating.
- Filter the movies to show only those released after 2010.
Tuples are used to store multiple items in a single variable.
A tuple is a collection which is ordered and unchangeable.
- Tuple items have a defined order, and that order will not change.
- We cannot change, add or remove items after the tuple has been created.
Tuple items are indexed, the first item has index [0], the second item has index [1] etc. - Tuple indexes enable duplicate values.
Tuples are written with round brackets.
Creating a Tuple
fruits = ("apple", "banana", "cherry", "blueberry")
print(fruits)or we can use the tuple() constructor
fruits = tuple(("apple", "banana", "cherry", "blueberry"))
print(fruits)Accessing Items
print(fruits[0]) # First item
print(fruits[-1]) # Last item
print(fruits[0:2]) # Range = first 3 items
print(fruits[:2]) # Range = first 3 items
print(fruits[1:]) # Range = last 3 itemsUnpacking Tuples
fruits = tuple(("apple", "banana", "cherry", "blueberry"))
(Central_Asia New_Guinea Northern_Hemisphere North_America) = fruits # Unpack Tuple itemsIterating Through a Tuple
for fruit in fruits:
print(fruit)or use tuple index numbers
for i in range(len(fruits)):
print(fruits[i])Tuple Methods
count() # Returns the number of times a specified value occurs in a tuple
index() # Searches the tuple for a specified value and returns the position of where it was found
- Dictionaries Cheat Sheet. https://www.cse.msu.edu/.../cheatSheet.pdf
- Python Sets. w3schools.com/python/python_tuples.asp
- Unpack Tuples. w3schools.com/python/python_tuples_unpack.asp
| Feature | List | Dictionary | DataFrame | Set | Tuple |
|---|---|---|---|---|---|
| Data Organization | Ordered, items by index | Key-value pairs | Rows and columns | Ordered, no dups | Ordered, immutable |
| Access Method | By index | By key | By row/column | Loop through | By index |
| Ideal Use Case | Simple collections | Mapping relationships | Tabular data | Resist duplicates |
Build a system that:
- Stores student data in a DataFrame.
- Allows adding a new student (Name, Age, Grade).
- Filters students by a minimum grade.
- Prints all student data.
Example Code for the System:
import pandas as pd
# Initial data
data = {
"Name": ["Alice", "Bob", "Charlie"],
"Age": [16, 17, 16],
"Grade": ["A", "B", "A"]
}
df = pd.DataFrame(data)
# Add a new student
new_student = {"Name": "Daisy", "Age": 17, "Grade": "A+"}
df = df.append(new_student, ignore_index=True)
# Filter by grade
print("Students with grade A or higher:")
print(df[df["Grade"] >= "A"])- Python Collections: docs.python.org/3/library/collections.html#module-collections
- Python Sequences: python.org/3/reference/datamodel.html#sequences
- Python Set Types: python.org/3/reference/datamodel.html#set-types
- Python Data Structures: python.org/3/tutorial/datastructures.html
- Sets: python.org/3/tutorial/datastructures.html#sets
- Tuples and Sequences: python.org/3/tutorial/datastructures.html#tuples-and-sequences
- More on Lists: python.org/3/tutorial/datastructures.html#more-on-lists
- Dictionaries: python.org/3/tutorial/datastructures.html#dictionaries
- Dictionaries: python.org/3/reference/datamodel.html#dictionaries
- Python Data Types: wikibooks.org/wiki/Python_Programming/Data_Types