2. More about lists
Definition: A generic term for a programming data structure that holds multiple
items. Essentially, it holds a group of elements.
Common Array Functions: insert (append), delete, length, sort (specific types of
sorting), and many more
Notes:
- All languages have some form of an array, many languages refer to this concept
as a list
- Memory and run-time efficient
3. Examples of Lists & Common Methods
# empty list
my_list = []
# list of integers
my_list = [1, 2, 3]
# list with mixed data types
my_list = [1, "Hello", 3.4]
# nested list (array inside of an array)
my_list = ["mouse", [8, 4, 6], ['a']]
* Question: How can we access "mouse" or ['a']? *
4. Adding & Removing Elements - Lists
Adding items to Lists:
myList = ['apple','mango']
myList.append('orange')
myList.append('grapes')
myList.append('strawberry')
myList.append('kiwi')
print(myList)
# Output:
['apple', 'mango', 'orange', 'grapes',
'strawberry', 'kiwi']
Remove Elements in a List:
myList1 = ['a','b','c']
myList1.remove('a')
myList1.remove('b')
myList1.remove('c')
# Output:
[]
Note* : Even though there are no elements
in the list, it still takes up memory, we
can still add elements!
5. Integrating Loops - Lists
Iterating Through Lists:
myList = ['apple','banana','mango']
for fruit in myList:
print("I like", fruit)
# Output:
I like apple
I like banana
I like mango
Combining Elements in 2 Lists:
myList1 = ['a','b','c']
myList2 = [1,2,3]
for value in myList2:
myList1.append(value)
# Output:
['a','b','c', 1, 2, 3]
6. Sorting & Length - Lists
Lists:
• A list holds ordered collection of items
• Items can be Strings, Integers, Floats, and custom Data Types
Sorting & Length:
• .sort() implementation orders the collection of items from decreasing to
increasing value, by default
• reverse property - (increasing to decreasing)
• key property - (user’s custom sort)
• len() - gives the total amount of elements in the collection (list)
7. Sorting & Length Examples - List
myList = []
myList.append('apple')
myList.append('grapes')
myList.append('carrots')
myList.sort()
print(myList)
# Output:
['apple', 'carrots', 'grapes']
myList.sort(reverse = True)
print(myList)
# Output:
['grapes', 'carrots', 'apple']
print('I have ', len(myList),' items
to purchase')
# Output:
I have 3 items to purchase
8. List Concept Practice Questions
Note: Please use your Repl accounts to test code and to save files for all classes
• 2 Questions
Question 1:
list1 = ["Hello ", "Hi"]
list2 = ["Goodbye", "Bye"]
Desired Output:
# Output:
List3 = ['Hello', 'Hi', 'Goodbye',
'Bye']
Question 2:
morningFoods = ["toast", "tea",
"apples"]
Desired Output:
# Output:
There was toast in breakfast
There was tea in breakfast
There was apples in breakfast
9. Dictionaries - Data Structure
Definition: A dictionary consists of a collection of key-value pairs. Each key-value
pair maps the key to its respective value.
Notes:
- Similar to a list in terms of methods that can be used
- Key (a), Value (b) paired
- Keys are represented using Strings only, Values can be any value
10. Dictionaries - Code Implementation
Dictionary Representation:
d = dict([(<key>, <value>),
(<key>, <value),
(<key>, <value>)])
MLB_team = dict([('Colorado', 'Rockies'),
('Boston', 'Red Sox'),
('Minnesota', 'Twins'),
('Milwaukee', 'Brewers'),
('Seattle', 'Mariners')])
Note* : Dictionaries can also be represented using:
d = {<key> : <value>, <key> : <value>}
MLB_team = {'Colorado' : 'Rockies', 'Boston', : 'Red Sox', …}
* Note: Dictionaries are different than lists!
Can’t be indexed using indices. Must be
accessed by keys.
MLB_team[1]
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
MLB_team[1]
KeyError:
11. Dictionaries - Retrieving Values
Printing Dictionary Values:
print('The Boston team name is', MLB_Team['Boston'])
# Output: 'The Boston team name is','Red Sox'
Deleting Dictionary Values:
del MLB_Team['Boston']
Printing Dictionary Values:
print(MLB_Team)
# Output: {'Boston': 'Red Sox', 'Milwaukee': 'Brewers', 'Colorado': 'Rockies', 'Seattle':
'Mariners', 'Minnesota': 'Twins'}
12. Dictionaries - Using Loops (Keys & Values)
Printing Keys & Values:
for key in MLB_team:
print(key)
print(MLB_team[key])
# Output: Boston Milwaukee Colorado
Seattle Minnesota
# Output: Red Sox Brewers Rockies
Mariners Twins
Checking For Specific Value:
for key in MLB_team:
if MLB_team[key] == 'Red Sox':
print('Found Boston')
break
else:
print(Missed Boston')
# Output: Found Boston
Note* : An alternative for quicker key, value search is:
if "Boston" in MLB_team:
print("Found Boston")
13. Dictionary Concept Practice Questions
Given Input:
d = {0: 10, 1: 20}
Desired Output:
- Update the dictionary,
such that it has the
given new integer set
# Output:
d = {0: 10, 1: 20, 2: 30}
Given Input:
d = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
Desired Output:
- Write a method, isKeyPresent, that determines whether or
not a given key is present in the dictionary. Assume the
key can be any type (String, Int, Float, etc..).
isKeyPresent(5) // true or false
isKeyPresent(9) // true or false
# Output:
True
False
14. Modules in Python
● Use multiple functions written by others
● Popular packages: numpy, pandas
● How to install these packages with Python?
import pandas
import numpy
15. Modules in Python - Numpy
• Useful for various array and matrix representation of elements
• Many functions are analogous to List data structure
import numpy as np
list_sample = [5,6,7,8]
numpy = np.array(list_sample)
print(numpy[0])
# Output: 5
16. Modules in Python - Pandas
• DataFrame: Two dimensional frame that has rows and columns and
multiple streams of input
import pandas as pd
df = pd.DataFrame({'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]});
print(df)
# Output:
X Y Z
0 78 84 86
1 85 94 97
2 96 89 96
3 80 83 72
4 86 86 83
17. Modules in Python - Pandas
• DataSeries: Two dimensional frame that has rows and columns, but
contains only one collection as input
import pandas as pd
ds = pd.Series([2, 4, 6, 8, 10])
print(s)
# Output:
0 2
1 4
2 6
3 8
4 10
Note* : Look at previous slide to
compare/contrast initializations of
DataFrame vs DataSeries
18. Modules in Python - Pandas
• Pandas is useful and important for reading CSV files, the datasets used for
training models
import pandas as pd
dataset = pd.read_csv(.csv’)
print(dataset.head())
print(dataset.dtypes)
• .head(n) - retrieves the top n (Integer) rows from the given dataset
• .dtypes() - retrieves the column title and its respective data type
19. Measuring Run Time of Code
import time
start_time = time.time()
//
main()
//
seconds = time.time() - start_time
print('Time Taken:', time.strftime("%H:%M:%S", time.gmtime(seconds)))
# Output: Time Taken: 00:00:08
● Main() is the function that contains the code to be executed
● We use the time library to ease with measuring run time, various
time zones, and more