Data Structures in Python: Lists and The Avengers
Introduction
Alright, I saw Infinity War and it was awesome. It was shocking, ridiculous and made me cry. Relax, there are some spoilers but there is a warning. I am going to steal some of the Avengers for my examples of python data structures, though. I’m not overly familiar with all the data structures that python has. As a challenge, I’ve come up with a plan to deep dive and examine the data structures within Python. The first in this series will be python lists.
Data structures are methods in which data is stored and handled. As python is a higher level language than say C we are not subject to memory management and garbage collection. Although, diving into making something like a list does assign memory addresses through the indexing process. We will talk more about that though.
Primitive VS Non Primitive Data Structures
Let’s examine how python classifies its data. Primitive data structures are structures that are handled within the programming language. They are the ints, floats, strings within python. They are also referred to as data types. Non Primitive data types in python refer to our data structures. Lists, tuples, sets, and dictionaries. They are structures that the programmer creates. As such when we index each of these and are assigning memory locations to the entries in the list. Memory locations are hardly something python exposes. It’s not really something you need to think about with a higher level language. However, knowing the underlying mechanics of a programming language, in my opinion, is important.
List Introduction
Lists are how we can store values in a list, referred to through the indexing of each of the values. Below is an example of a list:
list = [1,2,3] print (list)
Now we run our list
Our list is a set of numbers each with there own index. We can list a set of values for examples, names, dates, prices. There are many applications for our lists, one such application would be a set of strings to keep track of the heroes that join the Avengers. Here is the first round of Avengers from the first movie.
avengers = ['Iron Man','Captain America','Black Widow','The Hulk','Hawkeye','Thor', 'Falcon'] print ("Hero List example: ") print (avengers)
Then we run the code and get:
Hero List example: ['Iron Man', 'Captain America', 'Black Widow', 'The Hulk', 'Hawkeye', 'Thor', 'Falcon']
As seen above we can print out our list as one single list. We can also utilize a for loop for iterating through the list one by one. Typically if we are doing anything with this list (printing on a screen, adding to a database) we would use a for loop to iterate through the list
print ("For Loop Example: ") for x in avengers: print (x)
Then we run the code:
For Loop Example: Iron Man Captain America Black Widow The Hulk Hawkeye Thor Falcon
Stacking
We can use lists in many different ways. There are ways we can actually ‘stack’ our lists as well. Following a last, in first out method we can use the append and the pop functions for our list to add people and take them away. Those who saw Captain America: Civil War saw the addition two more heroes into the lineup.
avengers.append('Black Panther') print ("Append Example: ") for x in avengers: print (x)
Which gives us
Append Example: Iron Man Captain America Black Widow The Hulk Hawkeye Thor Falcon Black Panther
Now, we all know who the Black Panther is. He has a country to run. He doesn’t have time to sit around the Avengers compound and wait for something horrible to happen. As quickly as T’challa joins, he must step back, to fill the role of the King of Wakanda after his father’s death.
avengers.pop() print ("Pop Example: ") for x in avengers: print (x)
Which gives us:
Pop Example: Iron Man Captain America Black Widow The Hulk Hawkeye Thor Falcon
Stacking works just like this. The append method will add an entry at the end of the list. The pop method with removes an entry from the list. The difference with the pop method is that you can specify an index (position) of the value you want to be removed and it will remove it. If left blank the pop function will remove the last entry in the list.
Indexing
Indexing is the way you would keep track of the values and their order in the list. Starting from zero we count up, and that will give us the position of our entry. Let’s take Black Widow. Apart from being long overdue for a solo origin movie, we also need to find her position on the list.
print (avengers.index('Black Widow'))
Putting that command into the python interpreter and get 2 or the third position in the list.
Better Value Management
So the first to start to realize that they weren’t just that into The Avengers was Captain America. So let’s say we don’t know how the pop function works and we don’t have any idea where the Captain in on our list. We can use the remove function to remove it.
avengers.remove('Captain America') print ("Remove Example: ") for x in avengers: print (x)
The function finds and removes the given entry. If the value does not exist we get an error returned.
Remove Example: Iron Man Black Widow The Hulk Hawkeye Thor Falcon Black Panther
With the Cap and Iron Man on the outs, we got Black Panther inserted into the lineup. However, Iron Man also needed another hand in bringing Captain Rogers and his cohorts back into the fold. In comes the best movie version of Spider-Man. Let’s add him to the list. Say we wanted to make Spider-Man the new face of the Avengers, so we want him to put him as the first value in the list. We can do this using the insert function. Here we pass in the index position we want the value to appear in and the value.
avengers.insert(0, 'Spider Man') print ("Insert Example: ") for x in avengers: print (x)
If we print out our list one more time we see that Spider-Man appears at the very top. Which I’m cool with. Tom Holland makes an excellent Spider-Man/Peter Parker.
Insert Example: Spider Man Iron Man Captain America Black Widow The Hulk Hawkeye Thor Falcon Black Panther
How Many Spider-Men?
So the comic book universe has a dozen different Spider-Man variants. There’s Peter Parker Spider-Man, my favorite Miles Morales as Spider-Man, and the awesome Spider Gwen. The MCU (Marvel Cinematic Universe) only has the one Spider-Man for now. However, let’s count how many there are on our list to be safe.
We can use the count function for our list to count how many times a value appears in our list.
how_many_spider_mans = avengers.count('Spider Man') print ("Count Example Example: How Manny Spider Men are there?") print (how_many_spider_mans)
The MCU only has the one so our list only has the one Spider-Man value. So we get a one returned to us.
Count Example Example: How Manny Spider Men are there? 1
Nesting
We all loved the first Guardians of the Galaxy. The great visuals, the humorous nature of the dialogue and of course the somehow germane mix of 1970s music with space adventures. They exist separately in the Marvel universe apart from the Avengers on Earth. However, in Infinity War they all coalesce into some of the best dialogue in the MCU. So we have our list of heroes on Earth. Now comes another cool team to join them. So let’s create our new list.
gotg = ['Star Lord', 'Rocket', 'Groot', 'Drax', 'Gamora']
Now that we have our new list, we need to find a way to incorporate them into the Avengers, somehow. Python nested lists to the rescue. With python lists, you can nest lists inside of lists. There are applications for this. For example, keeping related information together in a list would be a great use. However, in that particular example, it may be better to use a class. More on that later. So let’s add the Guardians to the Avengers list.
avengers.append(gotg) print ("Append Nested List Example: ") for x in avengers: print (x)
After printing our list we see that we have the list of the Guardians along with the list of our other Avengers. Great job Kevin Feige!
Append Nested List Example: Iron Man Captain America Black Widow The Hulk Hawkeye Thor Falcon Black Panther ['Star Lord', 'Rocket', 'Groot', 'Drax', 'Gamora']
WARNING SPOILERS AHEAD!!
Now, this is a warning. If you have not seen Avengers: Infinity War please skip this. It’s a huge ending and if you care about the movies at all then please feel free to skip to the end. You’ve been warned.
SERIOUSLY, THERE’S A SPOILER AHEAD. IT’S A DOOZY.
Alright, you’ve been warned. For those of us who have seen Infinity War, we all know how it ends. It’s heartbreaking. I watched it completely in shock. I’m still processing. In the ending, most of the Avengers are killed off. Not all of them, however a good portion. So now we need to update our list. Here we can use the clear function to clear out our list of Avengers, as most of them no longer exist. Some in the list still exist in the MCU, however, using the end of the movie is fits better for this method.
avengers.clear() print ("Clear Example: ") print (avengers)
Which prints
Now when we print our list we get an empty list. I’m still not over it. Usually, those movies are not anywhere near as emotionally impactful. That’s changed since movies like Black Panther came out, which tackles huge complicated subjects in a nuanced and interesting way. I’m excited to see how the movies continue to raise the bar.
Conclusion
I hope working with the Avengers has helped to break down how to use lists and how they are used within a python program. There are many different applications for this data structure. There are however applications where a list may make sense but a different data structure would be more appropriate. We are going to explore other data structures in the coming parts of the series. I am excited to see what we can do.
Full source code can be found here: https://github.com/georGEO1989/Data-Structures-Lists