Iterating through the List Items

Iterating through the List Items

Lesson Details:
June 29, 2020


I: Introduction

A: Learn basic python programming for beginners

As a beginner, it is important to learn the underlying concepts underlying the programming languages before you actually learn how to program. In my case, I have been working on my Python skills as a hobby for a few years now. In this blog, I will walk through some of the basics of Python and how to use them in various scenarios.

While there are many tutorials on the internet that can help you learn Python, I am going to focus on what I consider the most fundamental concepts and why they are important. These include:

Variables and Data Types

Conditional Blocks and Loops

Conditional Blocks and Loops

Functions and Modules

Object Oriented Programming (OOP)

There are several other concepts such as strings, classes, etc. But, if you read the above four topics, you will be able to build fairly complex applications like web scraping or data analysis tools. There are also many more ways to loop through items such as lists and dictionaries, but I will just show one example of each below.

II: Body

A: Iterating through the list items

Lets say you want to print every item in a list:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Importing relevant modules for our script import sys , pprint def main (): names = [ 'John' , 'Smith' , 'Jane' ] for i in range ( len ( names )): name = names [ i ] print ( name , " " ) if __name__ == '__main__' : # Executing the script main () # Running the script $ ./example.py John Smith Jane John John John John John John John John John John John John John John John John John John John John John John John John John John John Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith Smith NameError : name 'Smith' is not defined $ ./example.py Traceback ( most recent call last ): File "./example.py" , line 23 , in < module> main () File "./example.py" , line 13 , in main for i in range ( len ( names )): NameError : name 'i' is not defined $ ./example.py Traceback ( most recent call last ): File "./example.py" , line 23 , in < module> main () File "./example.py" , line 13 , in main for i in range ( len ( names )): NameError : name 'i' is not defined $ ./example.py Traceback ( most recent call last ): File "./example.py" , line 23 , in < module> main () File "./example.py" , line 13 , in main for i in range ( len ( names )): NameError : name 'i' is not defined If you are following along with the tutorial, you should get an error while running your script. Lets go through these errors one by one: Line 1 says that it cannot execute the file since it doesn’t have permissions to run it. This is because we are using the terminal to run our code which runs everything at the root level - meaning everything has root level access. We can avoid this by running our code under Python Virtual Environment because this gives us a separate space where we can install our Python libraries without affecting other users or scripts. If you are new to virtual environments, check out the following tutorial which explains how to set up a Virtual Environment for Python 3 on your Mac OS X platform: Setting Up Python 3 Virtual Environment on Mac OS X Next, let’s see why we get NameError when we try to run our script: Line 13: We try to access i which does not exist so we get a NameError . Line 23: We try to access i again! This time we get a NameError because we already tried accessing it when we were trying to iterate through all the names in list names . Line 13: We try to access i again! This time we get a NameError because we already tried accessing it when we were trying to iterate through all the names in list Line 25: We try to access name again! Again, we get an error because we already tried accessing it when we were trying to iterate through all the names in list names . Line 13: We try to access i again! This time we get a NameError because we already tried accessing it when we were trying to iterate through all the names in list Line 27: We don’t need to know the index anymore since we only wanted the name of the person and not their index in the list, so we can remove this line from our script and it should work again: $ ./example.py John Smith Jane John John John John John John John John John John John John Jane Jane Jane Jane Jane Jane Jane Jane Jane While it may seem difficult at first, once you get used to using Python, it gets way easier! A good place to start learning about Python is here: https://docs.python.org/3/tutorial/index.html#introduction-to-the-language Another good resource is https://www.udacity.com/course/intro-to-programming--ud903 but this one is paid :) As a side note, a lot of people have asked me how you can find “free” resources online. My philosophy is that anything worth doing is worth paying for because people spend a lot of time creating content and deserve compensation for their efforts! In my opinion, Udacity is worth paying for because it costs less than a meal costs at McDonalds and you learn skills that can last you a lifetime :) B: Looping Through Dictionaries The syntax for dictionary loops is slightly different from lists because dictionaries do not have indexes like lists do. Instead, you provide a key that matches a value inside a dictionary. Listing 2 shows an example of a dictionary loop over a list of names: Listing 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Importing relevant modules for our script import sys , pprint def main (): people = { 'John' : { 'age' : 22 , 'name' : 'John Doe' }, 'Jane' : { 'age' : 19 , 'name' : 'Jane Doe' }, 'Smith' : { 'age' : 35 , 'name' : 'Smith Jone' }, } for key , values in people . items (): print ( key , " " ) for person in values : print ( person [ 0 ], " " ) if __name__ == '__main__' : # Executing the script main () # Running the script $ ./example_2.py 22 22 19 19 35 35 NameError : name 'people' is not defined $ ./example_2.py Traceback ( most recent call last ): File "./example_2.py" , line 23 , in < module> main () File "./example_2.py" , line 13 , in main for key , values in people . items (): NameError : name 'people' is not defined $ ./example_2.py Traceback ( most recent call last ): File "./example_2.py" , line 23 , in < module> main () File "./example_2.py" , line 13 , in main for key , values in people . items (): NameError : name 'people' is not defined $ ./example_2.py Traceback ( most recent call last ): File "./example

loader
Course content