Python 101: Data Structures

TK
The Renaissance Developer
4 min readJun 18, 2017

Part II: Array, Hash & Iteration through Data Structures

Here, in this second part of the Beautiful Python series by Tk, we will talk more about Python Data Structures, how it works & how we can iterate through it. If you didn’t see the first post from this series, you should definitely take a look!

List: Collection | Array | Data Structure

Imagine you want to store the integer 1 in a variable. But maybe now you want to store 2. And 3, 4, 5 …

Do I have another way to store all integers that I want, but not in millions variables? If I asked this question, it probably has another way to store it.

List is a collection that can be used to store a list of values (Like these integers that you want). So let’s use it!

It is really simple. We created an array and stored it on my_integer.

But maybe you are asking: “How can I get a value from this array?”

Great question. List has a concept called index. The first element gets the index 0 (zero). The second gets 1, and so on. You got the idea!

For better understand, we can represent the array and each element with its index. Should I draw it?

Using the Python syntax, it’s also simple to understand:

Imagine that you don’t want to store integers. You just want to store strings. Like a list of your relatives names. Mine would be something like that:

Works the same way as integers. Nice! :)

We just learnt how Lists indexes works, but I need to show you about how we can add an element to the List data structure (an item to a list).

The most common methods to add a new value to an List is append. Let see how it works:

append is super simple! You just need to pass the element (eg. “The Effective Engineer”) as the append parameter.

Well, enough of Lists. Let’s talk about another data structure.

Dictionary: Key-Value Data Structure

Now we know that Lists are indexed with integer numbers. But what if we don’t want to use integer numbers as indexes? Some data structure that we can use numeric, string, or other types of indexes.

Let’s learn about the Dictionary data structure. Dictionary is a collection of key-value pairs. Something that looks like it:

The key is the index pointing to the value. How do we access the Dictionary value? Yes, you are right. Using the key! Let’s try it.

I created a Dictionary about me. My name, nickname & nationality, and those attributes are the Dictionary keys.

As we learnt how to access the List using index, we also use indexes (keys in Dictionary context) to access the value store in the Dictionary.

In the example I printed a phrase about me using all the values stored in the Dictionary. Pretty simple!

Hmm, another cool thing about Dictionary is that we can use anything as the value. In the Dictionary I created, I want to add a key “age” and my real integer age in it.

A key (“age”) value (24) pair using string as the key and integer as the value.

As we did with Lists, let’s learn how to add elements to a Dictionary. The key pointing to a value is a big part of what Dictionary is, and so it is when we are talking about adding elements to the it.

We just need to assign a value to a Dictionary key. Nothing complicated here, right?

Iteration: Looping Through Data Structures

As we learnt in the Python Basics, the List iteration is very simple. We, Python developers, commonly use For looping! Let’s do it:

So for each book in the bookshelf, we (can do everything with it) print it. Pretty simple and intuitive. That’s Python! ❤

For hash data structure, we can also use the for loop but passing the key.

This is an example on how to use. For each key in the dictionary we print the key and its correspondent value.

Another way to do it is to use iteritems method.

We did name the two parameters as key and value, but it is not necessary. We can name it with “anything”. Let’s see it:

We can see we used attribute as a parameter for the Dictionary key, and it works properly! Great!

That’s it!

We learnt a lot of things about Python Data Structures:

  • List: Collection | Array
  • Dictionary Key-Value Collection
  • How we can iterate through this data structures

And that’s it guys! I want to update this article. The idea is to share a great content and the community helps improve it! ☺

I hope you guys can appreciate the content and learn more about how Python works. And for the third part of Beautiful Python series, we are gonna learn about Python Object Oriented Programming Part 1.

If you want a complete Python course, learn more real-world coding skills and build projects, try One Month Python Bootcamp. See you there! ☺

Have fun, keep learning & always coding!

I hope you liked this content. Support my work on Ko-Fi

My Twitter & Github. ☺

--

--