Wednesday, April 24, 2024
STEM

Array of dictionaries – Python datastructure

The internet is littered with simple examples of dictonary object examples in Python. Interestingly, they are all the same copy/pasted code with the same old tired examples and bad coding styles 😂. What we need is examples of more complex, real-world data structures that are more practical. In this blog, I share some valuable techniques to deal with an arrays of multiple dictionaries, where each dictionary item has multiple key-value pairs and and array within itself!

Sounds complicated, but when you think about it, you encounter this data structure pretty much everywhere in online shopping! You’ll soon see why.

¿sʇuɐꞁd ǝɯos ɥʇᴉʍ uɐɥʇ ǝpoɔ ǝɯos ʎɟᴉꞁdɯǝxǝ oʇ ʎɐʍ ɹǝʇʇǝq ʇɐɥʍ puɐ ʇsǝʍɥʇɹou ɔᴉɟᴉɔɐd ǝɥʇ uᴉ ǝɹǝɥ ɓuᴉɹds s’ʇI 🌺🌻🌹🌻💐🌸🍅🍆🧅🧄

Imagine you have an online store selling bulbs for flowering plants. For each item, you have a database somewhere with: itemid, name, price, quantity of bulbs included per package, and colors of the bloom so your customers can get exactly what they want. The data is served through the browser in a html format so the underlying data from the database has been transformed into probably some XML or JSON format. You are not only the owner of the store, but you also are the programmer 😁😁. You got the data from the database and now you need to enable some core features so you can interact with the user and ultimately the cart, and perhaps even the database indirectly. I won’t go into the database integration as it’s another huge topic by itself, but here I’ll focus just on the code necessary to create the correct data structure and manipulate it as needed. I’ll demonstrate some of the key activities you must know about in getting it done with Python.

First, your data strucutre could look like this:

The last statement (not part of the data structure of course) just shows what it looks like and the output is:

Take a closer look at bulbs variable. It has several items in it, each one looks like a row of data. But each ‘row’ sure looks like a dictionary! And it is. But it has several keys: itemid, name, unitprice, quantity, and color. Each key is assigned a value. That is the very definition of a Python dictionary. However, you notice something different about the ‘color’ key…its values are inside another array. This is necessary because each bulb you’re selling has various flower color properties and you want to show all the available options.

Your next challenge is to navigate this data structure, add, remove items from the bulbs catalog, and do some queries, sorting etc. We’ll get into all of those. But first, let’s see how we can get the number of items you offer in the catalog.

print(len(bulbs))

will give us the result: 6. Because len() works on a list, and bulbs is a list albeit a sophisticated one.

Next, you want to show an item at a specific position not knowing the itemid. To see the 4th item in the catalog (as the data is currently sorted/presented), you would do:

print(bulbs[3]['itemid'], bulbs[3]['name'], bulbs[3]['unitprice']) # out: 104 Ranunculus 6.95

(because arrays are 0-based index, so [3] is the 4th item)

Now we’re interested in knowing all the itemids. You can do this:

for i in range(0, len(bulbs)): print(bulbs[i]['itemid'])

You’ll get 101, 102, 103…106

You also want to see itemIDs, names and prices for each item in catalog. You can do this:

for i in range(0, len(bulbs)): print(bulbs[i]['itemid'], bulbs[i]['name'], bulbs[i]['unitprice'] )

To show all colors available for each item, you can do this:

for i in range(0, len(bulbs)): print(bulbs[i]['itemid'], bulbs[i]['name'], bulbs[i]['color'] )

Notice that I’m making it very verbose in the code by explicitly giving the starting index value (0) and ending index value (len(bulbs)) for the looping through the array. This is the best way to see what’s actually going on…you can clearly see that i is going from 0 to the number of items in the catalog and for each item in the array, it’s picking out the values by key names: ‘itemid’, ‘name’, etc.

If you want to make it more concise, you can actually let Python figure out the length and indexing for you, but I recommend you do that once you understand how you can do it yourself without relying on that. To do it the concise way, you would do this:

for i in bulbs: print(i['itemid'], i['name'], i['unitprice'], i['color'])

You’ll get the output as:

101 Tulip [‘red’, ‘white’, ‘yellow’]
102 Dahlia [‘red’, ‘blue’, ‘purple’]
103 Allium ['red', 'white', 'blue', 'assorted']
104 Ranunculus ['red', 'white', 'pink', 'assorted']
105 Hellebore ['yellow', 'white', 'red']
106 Clematis ['blue', 'white', 'pink', 'violet']

To show item names that are priced within a range, you can do this:

And you’ll get the results as:

102 Dahlia 7.95 2 ['red', 'blue', 'purple']
105 Hellebore 10.95 1 ['yellow', 'white', 'red']

To show total for a specific number of orders for a specific item by name. e.g. ‘Ranunculus’:

In the above example, if your customer had 10 orders for the said item, you can show the total. And the output is:

Item# 104 Ranunculus at $6.95 for 10 units. Total: $69.50

To show all items available in a specific color…e.g. ‘purple’:

The output is:

Item# 102 Dahlia $7.95 each. Colors: ['red', 'blue', 'purple']

You just added two new items to your catalog. To add them to your list, you will do this:

You just added Border Lily and Gladiolus bulbs to your catalog with their respective details.

You realize that you want to remove an item from your catalog named ‘Hellebore’. It’s not a bulb afterall.

You also realize that item# 106 is not a bulb either! So now you need to remove it by itemid instead of name. You can do this:

Wow, that was a lot of information and manipulations. However, with these examples, you should be able to implement any other variation here not listed. But I have one more to share…sorting!

Now you want to sort the catalog by name of the flowers/bulbs. You would do this:

What the above code is doing is creating a small function called x using lambda and sorting the entire list using a key name ‘name’ in ascending order. Note that this will mutate your original list. To not affect the original list, you can use sorted() function or create a copy of the current list by newlist=list(bulbs) for example and then sort the new list. In this case, we want it to be mutable so sort() is just right.

And your beautiful output sorted by name is:

Sorted List (by key:name) ASC:
{'itemid': 103, 'name': 'Allium', 'unitprice': 5.95, 'quantity': 5, 'color': ['red', 'white', 'blue', 'assorted']}
{'itemid': 107, 'name': 'Border Lily', 'unitprice': 9.99, 'quantity': 8, 'color': ['red']}
{'itemid': 102, 'name': 'Dahlia', 'unitprice': 7.95, 'quantity': 2, 'color': ['red', 'blue', 'purple']}
{'itemid': 108, 'name': 'Gladiolus', 'unitprice': 5.98, 'quantity': 10, 'color': ['red', 'white', 'pink']}
{'itemid': 104, 'name': 'Ranunculus', 'unitprice': 6.95, 'quantity': 3, 'color': ['red', 'white', 'pink', 'assorted']}
{'itemid': 101, 'name': 'Tulip', 'unitprice': 4.95, 'quantity': 5, 'color': ['red', 'white', 'yellow']}

Playing with these code snippets, you can master tackling these data structures. I hope this was useful and you’ll come back to learn more interesting stuff here! Your continued interest is my motivation to share more knowledge.



Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top