Understanding Python Data Structures (For Beginners)

Dallas Lind
Dec 2 · 6 min read

Photo by Roozbeh Eslami on Unsplash

After the past couple weeks of studying in my software engineering immersive, using Python in comparison to JavaScript is its own reward. While they’re both powerful languages with different utility, there’s still a refreshing feeling in typing out code that more closely resembles our usual written language. In addition, Python is a language that excels in fields of data science and analysis which is in part due to its system of lists, tuples, and dictionaries. This is going to be a brief overview of what each kind of structure specializes in and how to use it effectively. Let’s go!

Lists

A list is exactly how it looks, it’s a sequence of values and is considered by Python to be an object. Since it’s an object, this means it’ll be treated like other data types (think Boolean or Strings). Due to its nature, lists can be assigned a variable like in the example above.

An case by which a list would be used over other data structures is dependent on how you view yourself manipulating your data. For this type, it’s ordered and changeable, which is relevant in you’re interested in adding in additional content (or like the example above, add more action movies coincidentally set over the Holidays) or editing the list above. If you’re familiar with arrays, this next part will come naturally to you. To access any part of a list, you must call upon its index number. As another example, let’s say I want to pull out Lethal Weapon for some reason.

Remember, the way arrays work is that their indices start at 0, not 1, so to pull on the second item of the list it would be considered [1] instead of [2]. There’s also another method where you can specify a range of indices to return certain values if you don’t need all of them at the moment.

print(movie_list[2:5])

This method of pulling out the values you need will being with index 2 (Batman Returns) and goes through the range that ends at 4(Jingle All the Way). A little tricky though, since it stops at the one index right before the last (instead of index 5, it’ll end at index 4). Very cool, right? And hey, now you have some fun trivia around the holidays about Christmas-adjacent action movies!

There are many ways to manipulate the lists whether it’s by removing an item, inserting an item by index position, loop, and more! Again, this is a basic overview by which you’ll have a basic understanding of what lists are.

Tuples

So I know what you’re thinking. “What in the hell is a tuple?” I know, me too. Don’t worry, we’re going to go through it together! In contrast to a list, tuples are also ordered, but they’re unchangeable. A big difference in comparison to how lists treat their values. Let’s start off by showing an example of what a tuple is.

So from the get-go, there’s already a difference in the type of bracket it’s encased by. However, despite its round bracket identity, it still utilizes the rules of arrays! Yay, that’s not too difficult since we already went over it! Still, let’s provide a quick example of what this looks like. I’m craving plums right now, so let’s go ahead and pull that out of the tuple.

Look at that, that wasn’t difficult at all! It also follows the same structure if you’re interested in filtering out your tuple with [2:5] example we displayed earlier. While this is coming off as same-same, let’s not forget why lists and tuples aren’t the same thing. As stated earlier, tuples are unchangeable. Remember, you can’t add or remove items to a tuple, you will receive an error if you try it the same way you would with a list. There are ways to side-step that, but it is dependent on essentially changing the tuple to a list. Here’s an example of how to do that.

print(fruit)

The above is an example in how to change a value in a tuple by converting it into a list, pushing the new value over oranges => apples, then converting it back again into a tuple. This is why it’s important to have an idea of what you’d like to do with your dataset before choosing your data structure so these workarounds can be avoided if need be. Before we move on, here’s one more cool thing you can do with tuples. If you wanted to find a specific value in your tuple, this is a fabulous way to go about it.

Dictionaries

Alright, let’s try something a little different here. Dictionaries deviates from how lists and tuples operate in that each value will have their own unique key! Woah, that’s crazy! Let’s show a quick example to see what that looks like.

Let’s be real, I didn’t feel up to listing all of his movies, but here’s an example of a dictionary all about Quentin Tarantino! (My personal favorite is Inglourious Basterds because it’s objectively the best, don’t @ me) So you can see what I mean by each value having a unique key. Name refers to Quentin in the tarantino dictionary! Even better, nested lists are a viable option in dictionaries in how you present your data.

One of the nice things about data structures in Python is that in any of these data structure types, you can print your specific item by name and the unique identifier of the element. Let’s show a quick example of how that looks.

56

Thankfully, it’s fairly easy to access your values for each of these structure types, but it’s important to differentiate between their purposes and what you can and can’t do. For dictionaries, they’re unordered, changeable, and indexed. And again, to differentiate between these types, dictionaries are written with curly brackets as opposed to the square for list and round for tuple.

And just like tuple, you can utilize that cool method to see if there’s a specific key in your dictionary if you need to check. That being said, I encourage your printed statement to be less silly than mine here.

if "films_directed" in tarantino:
print("Yeah, he might've had a few 'films_directed")

Just like the other types, you can loop through and check length. And, just like lists, you can add or remove items to your dictionary as well. There’s also a way where you can create a dictionary that contains multiple dictionaries! That’s awesome!

Just to Wrap It Up

These are just some of ways you can interact with data structures in Python. Whether you want to compile a list of ingredients, a tuple of attendees, or a dictionary of an author’s career, there’s a myriad of ways you can utilize these for your data set. Remember to double check if your dataset needs to be changeable depending on what you need and have fun writing code! Listed below are some resources if you’d like to expand on the information from this article!

The Startup

Medium's largest active publication, followed by +536K people. Follow to join our community.

Dallas Lind

Written by

The Startup

Medium's largest active publication, followed by +536K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade