In our last lesson, we focused on bridging your JavaScript knowledge to Python's fundamental syntax, covering variables, control flow, and functions. You saw how Python's use of indentation enforces a clean, readable style and practiced translating a simple JavaScript function into its Python equivalent.
Today, we'll build on that foundation by exploring Python's core data structures: lists and dictionaries. These are the workhorses for managing collections of data in Python and are absolutely essential for virtually any task in AI and machine learning. We will also introduce list comprehensions, a powerful and distinctly "Pythonic" feature for creating lists concisely. For you, this will feel like a natural extension of working with arrays and objects in JavaScript, and we'll draw those parallels to help you get up to speed quickly.
Working with Ordered Data: Python Lists
In Python, the most common data structure for holding an ordered sequence of items is the list. A Python list is directly analogous to a JavaScript array: it's a mutable (changeable), ordered collection that can hold items of different types.
You create a list using square brackets [], just like a JS array.
# A list of numbers
primes = [2, 3, 5, 7, 11]
# A list with mixed data types
mixed_list = ["hello", 100, True, 3.14]
Accessing elements with an index (my_list[0]) and slicing to get sub-lists (my_list[1:4]) also work just as you'd expect. The "Bro Code" channel has a great video that walks through the basics of creating lists, accessing elements, and iterating through them with a for loop.
Python lists, sets, and tuples explained 🍍
Watch this video from "Bro Code" to get a practical introduction to Python lists.
Focus on these key segments: Creating a list and how it stores multiple values. Accessing elements using indexing and slicing, which is very similar to how you work with strings and arrays in JavaScript. Looping through a list using a for loop, a fundamental pattern for processing data.
Common List Operations
Just as JavaScript arrays have methods like push, pop, and splice, Python lists come with their own set of useful methods. The following video segment provides a great tour of the most important ones.
Python lists, sets, and tuples explained 🍍
Continue with the "Bro Code" video to learn about common list methods.
Pay close attention to these demonstrations: Changing elements in a list by assigning a new value to an index. .append(): Adds an item to the end of the list (like JS .push()). .remove(): Removes the first occurrence of a specific value. .insert(): Adds an item at a specific index (like JS .splice(index, 0, item)). .sort() and .reverse(): These modify the list in-place and return None. This is a key difference from JavaScript, where .sort() returns the sorted array. .index(): Finds the index of the first occurrence of a value.
For a complete reference, the official Python documentation is the best place to look.
5. Data Structures — Python 3.14.0 documentation
This section from the official Python tutorial provides a definitive list of all list methods. You don't need to memorize them all, but it's good to know what's available.
Skim through the list of methods to familiarize yourself with the available operations. Notice how methods that modify the list in-place, like sort(), have no return value.
Storing Key-Value Data: Python Dictionaries
The Python equivalent of a JavaScript object for storing key-value data is the dictionary. Dictionaries are incredibly important in Python for everything from simple data storage to configuring complex systems. They are unordered collections of key: value pairs, where each key must be unique and of an immutable type (like a string, number, or tuple).
You create a dictionary using curly braces {}.
# A dictionary mapping names to ages
user_ages = {
"Alice": 30,
"Bob": 25,
"Charlie": 35
}
# Accessing a value by its key
print(user_ages["Alice"]) # Output: 30
To get a hands-on feel for dictionaries, this next video is an excellent guide.
Python dictionaries are easy 📙
This "Bro Code" video provides a clear and practical walkthrough of Python dictionaries.
Watch these segments to understand the core functionality: Creating a dictionary with key-value pairs. Using .get() to safely retrieve a value without causing an error if the key doesn't exist. This is a very common and useful pattern. Updating and adding items using the .update() method or direct assignment. Removing items with .pop() and .clear(). Iterating with .items(): This is the most common way to loop through a dictionary's keys and values simultaneously. The for key, value in my_dict.items(): pattern is one you'll use constantly.
While lists and dictionaries are the two data structures you'll use most often, Python has others like tuples (immutable lists) and sets (collections of unique items). The table below gives a nice overview.

A More Elegant Way to Create Lists: List Comprehensions
In the last lesson, you wrote a function to filter even numbers from a list. It probably looked something like this:
def filter_evens(numbers):
results = []
for num in numbers:
if num % 2 == 0:
results.append(num)
return results
This works perfectly fine, but Python provides a more concise and readable way to accomplish this same task: a list comprehension. It lets you build a new list by applying an expression to each item in a sequence, optionally including a filter.
The basic syntax combines the loop and the conditional into a single line:

Using this syntax, we can rewrite our filter_evens logic in one line:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = [num for num in numbers if num % 2 == 0]
print(evens) # Output: [2, 4, 6, 8, 10]
If you have experience with JavaScript's map and filter array methods, you can think of list comprehensions as a way to combine their functionality.
[x * 2 for x in my_list]is likemy_list.map(x => x * 2).[x for x in my_list if x > 10]is likemy_list.filter(x => x > 10).
The official Python documentation has an excellent section on this topic with several powerful examples.
5. Data Structures — Python 3.14.0 documentation
This is the definitive guide to list comprehensions. It shows how they can simplify code that would otherwise require more verbose for loops.
Read the section on List Comprehensions. Pay attention to the initial example that creates a list of squares. Then, review the series of short examples that show how to use comprehensions for filtering, applying functions, and even flattening a nested list.
Your Turn: A Practical Challenge
Let's combine what you've learned. Imagine you have a list of dictionaries, where each dictionary represents a user with a name and a list of programming languages they know.
developers = [
{"name": "Alice", "languages": ["Python", "JavaScript"]},
{"name": "Bob", "languages": ["JavaScript", "C++"]},
{"name": "Charlie", "languages": ["Python", "Java"]},
]
Your task is to create a new list containing the names of only the developers who know Python. Try to do this using a list comprehension.
Click to see the solution
python_developers = [dev["name"] for dev in developers if "Python" in dev["languages"]]
print(python_developers) # Expected output: ['Alice', 'Charlie']
Breakdown:
dev["name"]: This is the expression. For each developer that meets the condition, we want to get their name.for dev in developers: This is the loop. We iterate through each dictionary in thedeveloperslist.if "Python" in dev["languages"]: This is the condition. We check if the string "Python" is present in thelanguageslist for the current developer.
Conclusion
In this lesson, we explored Python's fundamental data structures for handling collections of data. You've now gained practical experience with the tools you'll use daily in data science and machine learning.
Here are the key takeaways:
- Lists are Python's version of JavaScript arrays: ordered, mutable collections perfect for sequences of data.
- Dictionaries are Python's version of JavaScript objects: powerful key-value stores for mapping and lookups. You learned how to safely access data with
.get()and iterate efficiently with.items(). - List Comprehensions are a concise and efficient way to create lists from other iterables, often replacing multi-line
forloops. They are a hallmark of idiomatic Python code.
With a solid grasp of Python's basic syntax and its core data structures, you are now ready to start working with the specialized libraries that form the backbone of the data science ecosystem.
In the next lesson, we will cover how to import and use Python modules and packages, focusing specifically on NumPy and Pandas. You will see how these libraries build upon lists and dictionaries to provide highly optimized tools for numerical computing and data manipulation.