Input In [2]
"Favorite_Food": "unknown",
^
SyntaxError: invalid syntax
=======
[{'FirstName': 'John', 'LastName': 'Mortensen', 'DOB': 'October 21', 'Residence': 'San Diego', 'Email': 'jmortensen@powayusd.com', 'Owns_Cars': ['2015-Fusion', '2011-Ranger', '2003-Excursion', '1997-F350', '1969-Cadillac'], 'Favorite_Food': 'unknown', 'Owns': 'unknown'}, {'FirstName': 'Ellie', 'LastName': 'Pang', 'DOB': 'November 1', 'Residence': 'Del Sur', 'Email': 'elliepang007@gmail,com', 'Owns_Cars': ['No'], 'Favorite_Food': 'Sushi', 'Owns': 'Yes'}]
>>>>>>> f44d76edc589c60593655631253fadb963713168
</div>
</div>
</div>
</div>
Managing data in Lists and Dictionaries is for the convenience of passing the data across the internet or preparing it to be stored into a database. Also, it is a great way to exchange data inside of our own programs.
Next, we will take the stored data and output it within our notebook. There are multiple steps to this process...
-
Preparing a function to format the data, the print_data() function receives a parameter called "d_rec" short for dictionary record. It then references different keys within [] square brackets.
-
Preparing a function to iterate through the list, the for_loop() function uses an enhanced for loop that pull record by record out of InfoDb until the list is empty. Each time through the loop it call print_data(record), which passes the dictionary record to that function.
- Finally, you need to activate your function with the call to the defined function for_loop(). Functions are defined, not activated until they are called. By placing for_loop() at the left margin the function is activated.
For loop output
John Mortensen
Residence: San Diego
Birth Day: October 21
Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac
<<<<<<< HEAD
=======
Favorite_Food: unknown
Pet Owner: unknown
Ellie Pang
Residence: Del Sur
Birth Day: November 1
Cars: No
Favorite_Food: Sushi
Pet Owner: Yes
>>>>>>> f44d76edc589c60593655631253fadb963713168
<<<<<<< HEAD
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb Cell 7 in <cell line: 19>()
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=15'>16</a> for record in InfoDb:
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16'>17</a> print_data(record)
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=18'>19</a> for_loop()
/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb Cell 7 in for_loop()
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14'>15</a> print("For loop output\n")
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=15'>16</a> for record in InfoDb:
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16'>17</a> print_data(record)
/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb Cell 7 in print_data(d_rec)
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=5'>6</a> print("\t", "Cars: ", end="") # end="" make sure no return occurs
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a> print(", ".join(d_rec["Owns_Cars"])) # join allows printing a string list with separator
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a> print("\t", "Favorite_Food:", d_rec["Favorite_Food"])
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#W6sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8'>9</a> print("\t", "Owns_a_Pet:", d_rec["Pet_Owner"])
KeyError: 'Favorite_Food'
=======
>>>>>>> f44d76edc589c60593655631253fadb963713168
Alternate methods for iteration - while loop
In coding, there are usually many ways to achieve the same result. Defined are functions illustrating using index to reference records in a list, these methods are called a "while" loop and "recursion".
- The while_loop() function contains a while loop, "while i < len(InfoDb):". This counts through the elements in the list start at zero, and passes the record to print_data()
While loop output
John Mortensen
Residence: San Diego
Birth Day: October 21
Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac
<<<<<<< HEAD
=======
Favorite_Food: unknown
Pet Owner: unknown
Ellie Pang
Residence: Del Sur
Birth Day: November 1
Cars: No
Favorite_Food: Sushi
Pet Owner: Yes
>>>>>>> f44d76edc589c60593655631253fadb963713168
<<<<<<< HEAD
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb Cell 9 in <cell line: 11>()
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a> i += 1
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8'>9</a> return
---> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=10'>11</a> while_loop()
/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb Cell 9 in while_loop()
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=4'>5</a> while i < len(InfoDb):
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=5'>6</a> record = InfoDb[i]
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a> print_data(record)
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a> i += 1
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8'>9</a> return
/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb Cell 9 in print_data(d_rec)
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=5'>6</a> print("\t", "Cars: ", end="") # end="" make sure no return occurs
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=6'>7</a> print(", ".join(d_rec["Owns_Cars"])) # join allows printing a string list with separator
----> <a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=7'>8</a> print("\t", "Favorite_Food:", d_rec["Favorite_Food"])
<a href='vscode-notebook-cell://wsl%2Bubuntu/home/elliepang007/elliepang/_notebooks/2022-08-29-TP120-python_lists.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=8'>9</a> print("\t", "Owns_a_Pet:", d_rec["Pet_Owner"])
KeyError: 'Favorite_Food'
=======
>>>>>>> f44d76edc589c60593655631253fadb963713168
Calling a function repeatedly - recursion
This final technique achieves looping by calling itself repeatedly.
- recursive_loop(i) function is primed with the value 0 on its activation with "recursive_loop(0)"
- the last statement indented inside the if statement "recursive_loop(i + 1)" activates another call to the recursive_loop(i) function, each time i is increasing
- ultimately the "if i < len(InfoDb):" will evaluate to false and the program ends
Recursive loop output
John Mortensen
Residence: San Diego
Birth Day: October 21
Cars: 2015-Fusion, 2011-Ranger, 2003-Excursion, 1997-F350, 1969-Cadillac
<<<<<<< HEAD
=======
Favorite_Food: unknown
Pet Owner: unknown
>>>>>>> f44d76edc589c60593655631253fadb963713168
Ellie Pang
Residence: Del Sur
Birth Day: November 1
Cars: No
<<<<<<< HEAD
=======
Favorite_Food: Sushi
Pet Owner: Yes
While loop output
pasta
pizza
strawberries
chips
french fries
cherries
While loop output
cherries
french fries
chips
strawberries
pizza
pasta
While loop output
pasta
pizza
strawberries
chips
french fries
cherries
While loop output
french fries
chips
strawberries
pizza
pasta
0 pasta
1 pizza
2 strawberries
3 chips
4 french fries
5 cherries
What is my favorite Fruit?
is incorrect.
What is my favorite Vegetable?
is incorrect.
What is my favorite Soup?
is incorrect.
What is my favorite Drink?
is incorrect.
What is my favorite Snack?
is incorrect.
>>>>>>> f44d76edc589c60593655631253fadb963713168
Hacks
- Add a couple of records to the InfoDb
- Try to do a for loop with an index
- Pair Share code somethings creative or unique, with loops and data. Hints...
- Would it be possible to output data in a reverse order?
- Are there other methods that can be performed on lists?
- Could you create new or add to dictionary data set? Could you do it with input?
- Make a quiz that stores in a List of Dictionaries.
</div>