Looping basics in Python

One thing you will come across a lot in python is having to loop. Being able to understand loops is quite important as there is nothing worse than going loopy over loops.

What is looping

Looping is also known as iterating, essentially we go through a list and apply some logic. A list is python is a type and if your coming from none python land its an array. A list can hold most data types. We can also iterate objects which are not lists such as tuples, dictionaries and sets, we can also iterate strings.

For the most part we would usually iterate lists or dictionaries or tuples, a list implies multiple items and is the most intuitive concept to grasp (as its like a shopping list).

Most examples I see literally show shopping lists and are very impracticable or not really real world scenarios, for me this never really helped as I always like seeing something being used in a real life scenario as it provides inspiration for how I might be able to use it.

scenario

OK for the scenario we are going to take the following.

We have a requirement to add a customer to a database table via an API. We need to build an API request to add the details.

We can either:

Assume they only ever want to add one customer ever at a time.

Future proof ourselves and allow adding multiple.

For me the second option seems most sensible, by providing the functionality of adding multiple and treating the request as a set of data we can make use of lists (And dictionaries).

Plan

The plan is to build a data set, for this scenario lets say we require the following fields:
Name, contact, address

For the purpose of this we are storing this all in a flat table.

The single approach would be

{
  "name": "xxx",
  "contact": "123",
  "address": "My Address"
}

If we passed this data to our API via JSON we could insert the single row.
As mentioned previously a list can contain other data types (They also dont have to all be the same but for this they will be). Simply enclosing the dictionaries in a list means we can have any number of customers.

[
  {
    "name": "xxx",
    "contact": "123",
    "address": "My Address"
  },
  {
    "name": "xxdx",
    "contact": "12d3",
    "address": "My Addrdess"
  }
]

Ok so far it hopefully makes sense, now from the API side or receiving side how could we go through this data and extract each entry?

This is where we loop.

Looping the data

We can now loop the data, ideally we first check it is a list etc.

To loop and extract each customer we do this

for customer in data:
  print(customer['name'])

This code loops the variable named data (We are of course not running an actual api this is just a scenario example). the for customer bit states that every iteration in data will be referenceable by the variable we are declaring called customer, this could be named anything but we aim to give it context. We can then access the dictionary for the item we are on and then print its values for the spcecific key we want. In this case we are just printing the customer name.

The output would be

xxx
xxdx

The idea being we could then apply some logic and check the name is valid, length etc and then use this data to then complete the database insert.

Conclusion

This is a brief very basic example which I hope explains looping in a way in which people can grasp, the fundamental concept is that we go through a list of items and access the data each iteration.
The main point is knowing when to use them and seeing that it makes much more sense to have a list of items when multiple might be an option (Even if not so immediately).

Leave a Reply

Discover more from Secnetlinux

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Secnetlinux

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top