Data Structures
Hey there, welcome back to the Python basics tutorial! Today I'm going to be teaching you about Data Structures.
If you're freaking out by the name "Data Structures", calm down. We're not going to be messing with huge numbers here.
A data structure is a way to store, organize and manipulate your data.
Lists
Let's say you have to store the usernames of 5 people. Sure, you can create 5 variables for each user, but if there was a new user? Then you'd have to figure out how to make a new variable with the users name from a string. A simpler way here would be to make a "list" of usernames. How do you make a list?
We're defining a list by putting values separated by commas inside a pair of square brackets. We can store integers too:
Or, you can mix them:
A list is a data structure.
You can access values stored in a list by mentioning its place or index in the list. You access them by using their index inside a pair of square brackets as well.
Important thing you should always remember: List indexes start from 0, not from 1, as you might expect. So, the first element in a list is actually the 0th element. It might seem confusing, so let's see it being used.
Easy enough. You can also change the value of a list's element by their index:
You can "nest" or put a list in another.
You can create empty lists too:
You'll learn more about lists later in the chapter.
Lists are called arrays in other programming languages.
A list is only one data structure. There are a few more, tuples, dictionaries and sets.
Tuples
Tuples are a lot like lists, except they're constant. You can only create a tuple, but not modify it. You can index a tuple just like lists, but assigning won't work. You create a tuple just like a list, but with parentheses ().
Dictionaries
So far, you've been indexing lists and tuples via numbers. What if you wanted to get the age of a user named 'Mike' from a data structure? It would be hard to manually specify the indexes, so you can use dictionaries. They're created with {curly braces} and have a key == value structure. You can access it like this: dictionary[key]
to get value
.
Simple. You can also assign values like:
You can also nest dictionaries.
Sets
Sets are yet another kind of data structure. They only store unique values, so you can not store twins called Mike in a set. You can create a set like this:
But if you add duplicate elements:
You'll see that there's only 1 Mike. Poor twins.
Which one should I use, and when?
When you just want a list of values, well, use a list
. If you want a constant list, whose values never change, use tuples. When you need to access values by specific keys, go for dictionaries. Sets are more "memory efficient", so when you have all-unique values and need speed, use sets.
In most cases you'll probably want to use a list.
List operations
You must be thinking, we really can just store and access data from lists and tuples. And adding values with list[index] = value
isn't really great. Luckily, Python has a BUNCH of useful operations we can use to manipulate lists. Suppose you have to add a username to the end of your list. You can manually find out the next index and assign the value to that, OR you can use append()
:
MUCH better, right? You can use len()
to find the length of lists, tuples, dictionaries and sets:
len()
can be used on strings too.
You can use index()
to find the index of a specific value in a list.
Or insert()
to insert an element at a given index:
And remove()
to remove the first match of the given element:
And pop()
to remove values at a specific index:
Let's not forget:
reverse()
- Reverse the list.
sort()
- Sort the list.
max()
and min()
to get the maximum and minimum values of a list respectively.
We can use .join()
to join the elements of a list together with a specified delimiter or separator:
It's used like: string.join(list)
and joins the elements of the list together with string
separating them.
You must've noticed that sometimes we do list.function()
and sometimes function(list)
. In the first one, function
is actually a method. A method is actually a regular function "bound" to an object
such as a list. (You'll learn more about this in next chapters.) And the second one is just a regular function.
We can use the in
logical operator to determine if an element is in a list:
We can use in
with strings too.
We've barely scratched the surface! There are TONS of other cool stuff you can do with lists, but that's your homework.
Conversion between data structures
Another important thing I should say before ending this chapter: How to convert between data structure types? Well, that sure is easy enough.
To convert something to a list: list(other)
To convert to a tuple: tuple(other)
Converting to a dictionary from a list or tuple isn't that simple, so we'll look at it later.
To convert to a set: set(other)
Whew! That was a long chapter, but I hope you've got the gist of data structures, why and how to use them! If you face any problems, try to google it first. If that doesn't work, you can always contact me!
Last updated
Was this helpful?