Python Data Structures Simplified for Laravel Developers

Transitioning from PHP to Python for Machine Learning feels a bit like moving from a familiar manual car to an electric one. The controls are in similar places, but the way the engine feels under the hood is fundamentally different.
As Laravel developers, we are spoiled by the PHP array. It’s our everything tool.
Need a list? Array. Need a map? Array. Need a stack? Array.
Python, however, prefers specialized tools for specialized tasks. This is the secret to why it handles massive ML datasets so efficiently.
Here is your guide to mastering Python data structures without losing your Laravel mindset.
1. Lists: Your New Indexed Array
In Laravel, when you’re fetching a collection of names, you’re working with an indexed array. In Python, this is a List.
The PHP Way
$frameworks = ['Laravel', 'Symfony', 'CodeIgniter'];
$frameworks[] = 'Lumen'; // Push to end
echo $frameworks[0]; // Laravel
The Python Way
frameworks = ['Laravel', 'Symfony', 'CodeIgniter']
frameworks.append('FastAPI') # Using a method instead of []
print(frameworks[0]) # Laravel
The ML Advantage: Slicing
In ML, you often need to "split" your data (e.g., take the first 80% for training). Python makes this incredibly easy compared to array_slice().
# Python "Slicing" syntax: [start:stop]
top_two = frameworks[:2] # ['Laravel', 'Symfony']
last_one = frameworks[-1] # 'FastAPI' (Negative indexing is a lifesaver!)
2. Dicts: The Associative Array
When you’re defining an $user object in a controller, you use an associative array. In Python, we call this a Dictionary (or Dict).
The PHP Way
$config = [
'driver' => 'mysql',
'host' => '127.0.0.1'
];
$driver = $config['driver'];
$port = $config['port'] ?? 3306; // Null coalesce
The Python Way
config = {
'driver': 'mysql',
'host': '127.0.0.1'
}
driver = config['driver']
port = config.get('port', 3306) # The "get" method handles missing keys gracefully
The Human Touch: In Python, keys are typically strings or numbers. You’ll use these constantly in ML to hold Hyperparameters the settings for your models (like {'learning_rate': 0.01, 'epochs': 10}).
3. Tuples: The One You’re Missing
This is the one structure that doesn't have a twin in PHP. A Tuple is essentially an array that cannot be changed once it's born (it's "immutable").
Why would you want that?
Imagine you have an RGB color for a brand or geographic coordinates for a store. You don't want your code to accidentally change the Latitude halfway through a calculation.
# Defined with parentheses
coordinates = (34.0522, -118.2437)
# This will throw an error!
# coordinates[0] = 35.0
Pythonic "Unpacking":
You know how we use list() or [] In PHP, to destructure? Python does this natively and beautifully.
// PHP Destructuring
[$lat, $long] = [34.0522, -118.2437];
# Python Unpacking
lat, long = coordinates
print(f"Lat: {lat}, Long: {long}")
The "Laravel to Python" Cheat Sheet
| Task | Laravel (PHP) | Python |
| Count items | count($items) | len(items) |
| Check for key | array_key_exists('k', $arr) | 'k' in items |
| Merge data | array_merge($a, $b) | {**a, **b} (for dicts) |
| Get all keys | array_keys($arr) | items.keys() |
| Looping | foreach($arr as $k => $v) | for k, v in items.items(): |
Exercise: The "Laravel Config" Challenge
To get these into your muscle memory, try this:
Create a Dict named
app_settingscontaining aname,env, and a List ofproviders.Add a new provider to the list.
Use
.get()to try to print aversionkey that doesn't exist yet, providing1.0as a default.Create a Tuple for your database credentials
(host, port, user)and unpack them into three separate variables.


