You are on page 1of 3

1

 Lists

- Used to define an empty list

- This code adds a dictionary named “grocery_item” to a new position in the existing list
“grocery_history”

- This code iterates through every dictionary in the list “grocery_history”

- This code retrieves values stored within the dictionary at a specific index “item” of the
list “grocery_history”

 Dictionaries

- Used to define a new dictionary

- This block generates a new key-value-value trio for the dictionary “Grocery_item”

- Retrieves values stored within the dictionary “item” which itself is stored at an index
position in the list
2

- Retrives values stored in the dictionary “item”

 Loops

- Creates a while loop which runs until the user enters 1 to quit

- Creates a for loop which iterates through every item(dictionary) stored in the list
“grocery_history”

Loops are used when we wish to repeat a given block of code numerous times before
proceeding onto the next block. While loops are used when we wish to ensure our code meets
one or more specific conditions before the next block is run. These loops require an escape
condition to ensure the loop does not run indefinitely. This makes while loops important for
handling user errors such as wrong input. These can also be used to raise error messages if
try/except blocks are not feasible.
For loops are used to iterate over a specified range of values. For every value iterated,
the code within the for loop block is executed. For loops are often used when we need a variable
to take upon multiple values, one after another (eg For x in range 13:). Mathematical calculations
such as factorials use for loops in this form. Another common use of for loops is to iterate over
contents of a list or dictionary. This can be used for a range of activities such as Concatenation,
summing values, generating minimum and maximum numbers, etc (Python | Ways to
Concatenate Two Lists). For loops often lead to errors such as overwriting values in variables.
On the other hand, for loops help avoid multiple errors as the code only needs to be corrected in
one place rather than several. Catching errors is also easier as the code is much more concise
than it would be if blocks were repeated. In general, it is best to use loops whenever similar
processes need to be run several times in quick succession. For loops are best used when the
iterations of the loop are limited. While loops are best used for situations with no defined number
of iterations.
3

Lists are mutable collections of values stored in a specific sequence. They can be used
to store values of different types. They are best used in circumstances when we are required to
edit, add or remove data within the same data structure in our code. Lists are commonly used
when we wish to maintain a sequence of elements in our data, for example, a sequence of lines
read from a text file. They are the most commonly used structure for storing other data
structures. Lists are used for debugging errors found in data imported from external files or
online sources. An example of this is the use of lists by the beautiful_soup library to debug
HTML code (Beautiful Soup Documentation). Lists automatically attach an index key with every
element, thus helping the programmer correct errors caused by the use of nominal data. They
also help avoid overwriting data as their default manner of operation is to append new data next
to old data. In this manner, they can be used for basic forms of version control as well.
Dictionaries are pairs of keys and values that are stored in an unordered sequence.
These are best used in cases where we have a unique manner of identification (key) associated
with similar types of values in our data. They can be used for aggregation of all values associated
with a key. Dictionaries can be a good way of storing tabular data. They help avoid tracebacks
raised due to the handling of multiple types of values. They can be used to store multiple inputs
from the same user.

Bibliography

Beautiful Soup, Retrieved from https://www.crummy.com/software/BeautifulSoup/bs4/doc/,


accesses 4th August 2020

Python | Ways to Concatenate Two Lists - GeeksforGeeks, Retrieved from


https://www.geeksforgeeks.org/python-ways-to-concatenate-two-lists/, accesses 4th
August 2020

You might also like