LEARN TO CODE > PYTHON > LISTS
|
NEXT PAGE >
FUNCTIONS AND PROCEDURES |
WHAT ARE LISTS ?
Lists are just like the normal variables we have already covered except they can hold multiple items of data. In Python lists are called lists, however in many other programming languages they are known as Arrays, this is important to remember as they are normally called arrays and collections in examination papers.
You can declare a list with the same naming rules as a normal variable. In this section we will cover Lists, 2 dimensional lists, tuples and dictionaries.
LISTS - Like a variable but you can store multiple strings within a list
TUPLES - Like a list but once created you cannot change the content
DICTIONARIES - Like a list but each value in the dictionary has a corresponding key to value
You can declare a list with the same naming rules as a normal variable. In this section we will cover Lists, 2 dimensional lists, tuples and dictionaries.
LISTS - Like a variable but you can store multiple strings within a list
TUPLES - Like a list but once created you cannot change the content
DICTIONARIES - Like a list but each value in the dictionary has a corresponding key to value
WATCH THIS VIDEO TO GUIDE YOU THROUGH THIS SECTION
SECTION 1 | INTRODUCTION TO LISTS
CREATING A LIST
A List is an array of data that can be created and easily edited. A list is defined by the use of square brackets [ ] and you need to assign the list to a variable. Each element in a list has a numeric position and the positions start from 0: The example below:
A List is an array of data that can be created and easily edited. A list is defined by the use of square brackets [ ] and you need to assign the list to a variable. Each element in a list has a numeric position and the positions start from 0: The example below:
bob = ["fish", "feet", 5, "Cats"] print (bob) #Would print the entire list print(bob[0]) #Would print the first item in the list "fish" print(bob[1:3]) # Would print print the range of items in positions 1 - 2: "feet", 5
Notice how the last print statement it does not print position 3, it prints up to 3 but not including 3.
SECTION 2 | LIST METHODS
EMPTY LISTS You can create an empty list to be populated at a later stage or through a loop process:
myList = [ ]
APPEND METHOD An example of populating a list with 3 names from a user input using the append method is given below.
The line two lines below show an empty list being created then an item being placed into the last position of the list using the append method.
studentNames = [ ]
studentNames.append("Bob")
The append method is great when taking use input or appending in loops as can be seen in the example below.
myList = [ ]
APPEND METHOD An example of populating a list with 3 names from a user input using the append method is given below.
The line two lines below show an empty list being created then an item being placed into the last position of the list using the append method.
studentNames = [ ]
studentNames.append("Bob")
The append method is great when taking use input or appending in loops as can be seen in the example below.
studentNames = [ ] for i in range (3): name = input ("Please enter the name of student " + str(i + 1) + ": ") studentNames.append(name) print (studentNames)
Copy, paste and try the code above. You will see it uses the i from the loop to give a user-friendly statement. Each iteration of the loop adds the user input to the list.
FINDING THE LENGTH OF A LIST
Quite frequently you will not know how many items are in the list, we can use the len() method to find the length of a list. By using the len() method you can start to create more dynamic programs.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"]
print (len(studentNames))
The output would be 5 because there are 5 values in the list, remember that the 5th value is actually in index 4 because the count starts at 0.
Looking at a practical example of using the len() method.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"]
for i in range (len(studentNames)):
print (str(i+1) + ": " + (studentNames[i]))
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"]
print (len(studentNames))
The output would be 5 because there are 5 values in the list, remember that the 5th value is actually in index 4 because the count starts at 0.
Looking at a practical example of using the len() method.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"]
for i in range (len(studentNames)):
print (str(i+1) + ": " + (studentNames[i]))
CLEAR METHOD
Use the clear method to remove all items from a list. For example:
Use the clear method to remove all items from a list. For example:
starwars = ['IV', 'A New Hope', '1977','V', 'The Empire Strikes Back', '1980'] starwars.clear() print (starwars)
Copy, paste and try the code above. You will see that the original list is cleared and two empty brackets [ ] are printed.
COPY METHOD
Use the copy method to make a direct copy of an existing list, this is often useful when you do not want to change the original list, you can make any changes to the copy of the list instead.
Use the copy method to make a direct copy of an existing list, this is often useful when you do not want to change the original list, you can make any changes to the copy of the list instead.
starwars = ['IV', 'A New Hope', '1977','V', 'The Empire Strikes Back', '1980'] moviesWatched = starwars.copy()
COUNT METHOD
Use the count method to count the number of elements with the specified value.
Use the count method to count the number of elements with the specified value.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" ,"Jack" ,"Ben", "Jane", "Paul", "Fran", "Sally"] inList = studentNames.count("Jane") print (inList)
The count function is very useful if you just want to check if an item is in a list, for example you could ask the user for a value they want to check then return a statement to tell the user if the value is or is not in the list.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" ,"Jack" ,"Ben", "Jane", "Paul", "Fran", "Sally"] findName = input("Please enter the name that you want to check is in the list : ").title() inList = studentNames.count(findName) if inList > (1): print (findName + " is in the list " + str(inList) + " times.") elif inList > (0): print (findName + " is in the list " + str(inList) + " time.") else: print (findName + " is not in the list")
Copy, paste and try the code above. You will see it takes the users input, converts it to the same format as in the list (first letter capital), then prints a user friendly output using the input variable and the count variable within a string.
EXTEND METHOD
Use the extend method to add elements of a list to the end of another list. For example.
Use the extend method to add elements of a list to the end of another list. For example.
studentNames1 = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames2 = ["Ben", "Jane", "Paul", "Fran", "Sally"] studentNames1.extend(studentNames2) print (studentNames1)
Copy, paste and try the code above. You will see it combines the two list together to form one list called studentNames1.
INDEX METHOD
Use the index method to return the index of the first element with the specified value. As you get more familiar with using lists you will start to care more about the index of the list rather than the data within in. The index method is a useful command.
Use the index method to return the index of the first element with the specified value. As you get more familiar with using lists you will start to care more about the index of the list rather than the data within in. The index method is a useful command.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] namePosition = studentNames.index("Bill") print (namePosition)
Copy, paste and try the code above. You will see it prints '2', because 'Bill' is in list index (position) 2.
INSERT METHOD
Use the insert method to insert an item into a list in a specified position
Use the insert method to insert an item into a list in a specified position
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames.insert(3,"Sophie") print (studentNames)
Copy, paste and try the code above. You will see that Sophie has been placed in list position 3, changing the studentNames list to:
['Dave', 'Jane', 'Bill', 'Sophie', 'Bob', 'Jack']
['Dave', 'Jane', 'Bill', 'Sophie', 'Bob', 'Jack']
POP METHOD
Use the pop method to remove an element from a specified list index (position). Again very useful as often you do not need to be concerned when is in the list, and will often find yourself manipulating list based on the list index rather than the content.
Use the pop method to remove an element from a specified list index (position). Again very useful as often you do not need to be concerned when is in the list, and will often find yourself manipulating list based on the list index rather than the content.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames.pop(3) print (studentNames)
Copy, paste and try the code above. You will see that 'Bob' has been removed from the list.
REMOVE METHOD
Use the remove method to remove an element from a list based on its value.
Use the remove method to remove an element from a list based on its value.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames.remove("Bob") print (studentNames)
Copy, paste and try the code above. You will see that 'Bob' has been removed from the list.
DEL METHOD
The del method can be used to remove a range of items from a list. In the example above index 0 and 1 will be deleted from the list
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] del studentNames[0:2] print (studentNames)
Copy, paste and try the code above. You will see that 'Bob' has been removed from the list.
REVERSE METHOD
Use the reverse method to reverse the order of a list.
Use the reverse method to reverse the order of a list.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames.reverse() print (studentNames)
Copy, paste and try the code above. You will see that this list is printed in reverse. NOTE: You might expect to be able to make a new list equal the reverse of the list by doing something like: myNewList = studentNames.reverse(), however this does not work as expected. Instead if you need a copy of the list in reverse and a copy in the original order then you could do:
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] myNewList = studentNames.copy() myNewList.reverse() print (myNewList)
Copy, paste and try the code above. You will see that you now have the original list in the original order and the new list in reverse order.
SORT METHOD
Use the sort method to sort lists into numeric of alphabetical order.
Use the sort method to sort lists into numeric of alphabetical order.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] studentNames.sort() print (studentNames)
Copy, paste and try the code above. You will see that the list is sorted into alphabetic order, the same will work for a numeric list.
JOIN METHOD
The join method is not strictly a list method, it will work equally as good with strings but it can be very useful to use with lists, it allows you to print the list as one string, on one line and choose what you want separating each element of the list, in the example below there will be 4 spaces(1 tab) separating each element after the join method is used, anything you place in the ' ' will be inserted inbetween each element of the list as it is joined. The statement immediately before the .join determines the separating data.
The join method is not strictly a list method, it will work equally as good with strings but it can be very useful to use with lists, it allows you to print the list as one string, on one line and choose what you want separating each element of the list, in the example below there will be 4 spaces(1 tab) separating each element after the join method is used, anything you place in the ' ' will be inserted inbetween each element of the list as it is joined. The statement immediately before the .join determines the separating data.
studentNames = ["Dave", "Jane", "Bill" ,"Bob" , "Jack"] newList = ' '.join(studentNames) print (newList)
Copy, paste and try the code above. You will see that list will print as a string with 4 spaces between each element.
REMOVE DUPLICATES FROM A LIST
A quick and easy way to remove duplicates from a list.
myList1 = ["Bob", "Jane", "Jenny", "Bob"]
myList2 = (list(set(myList1)))
print(myList2)
The set() function is used to create a set object, which is an unordered collection of unique elements. A set is mutable, iterable, and does not allow duplicate elements.
The set() function can be called with an iterable (such as a list, tuple, or string) as an argument. It iterates over the elements in the iterable and creates a set by adding unique elements to it.
A quick and easy way to remove duplicates from a list.
myList1 = ["Bob", "Jane", "Jenny", "Bob"]
myList2 = (list(set(myList1)))
print(myList2)
The set() function is used to create a set object, which is an unordered collection of unique elements. A set is mutable, iterable, and does not allow duplicate elements.
The set() function can be called with an iterable (such as a list, tuple, or string) as an argument. It iterates over the elements in the iterable and creates a set by adding unique elements to it.
COUNT ITEM OCCURANCES AND REMOVE DUPLICATES
This is a simple method to count how many times an item occurs in a list and remove any duplicate items.
SECTION 3 | 2 DIMENSIONAL LISTS
Using a two dimensional array can be a great way to store and manipulate your data. This video talks you through using 2D Arrays.
WATCH THIS VIDEO TO GUIDE YOU THROUGH THIS SECTION
THE CODE FROM THIS VIDEO CAN BE SEEN BELOW
THE CODE FROM THIS VIDEO CAN BE SEEN BELOW
drivers = [ 1, "Hamilton", "MERCEDES",132, 2, "Verstappen", "RED BULL RACING HONDA", 95, 3, "Bottas", "MERCEDES", 89, 4, "Leclerc", "FERRARI", 45, 5, "Stroll", "RACING POINT BWT MERCEDES", 40] start = 0 end = 4 for i in range (3): print (drivers[start:end]) start = end end = end + 4 #print (drivers[0:4]) #print (drivers[0:12]) #print (drivers[0:4]) The code below shows using the 2 dimensional Array method. It is probably more simple than using a 1 dimensional array for the same task. constructors = [ [1,"MERCEDES", "Bottas", "Hamilton", 221], [2,"RED BULL RACING HONDA","Albon","Verstappen",135], [3,"RACING POINT BWT MERCEDES", "Perez","Stroll",63], [4,"MCLAREN RENAULT","Norris","Sainz",62], [5,"FERRARI","Leclerc","Vettel",61], [6,"RENAULT","Ocon","Ricciardo",36], [7,"ALPHATAURI HONDA","Gasly","Kvyat",16], [8,"ALFA ROMEO RACING FERRARI","Giovinazzi","Raikkonen",2], [9,"HAAS FERRARI","Magnussen","Grosjean",1], [10,"WILLIAMS MERCEDES","Russell","Latifi",0] ] for i in range (3): print ("Team " + (constructors[i][1]) + " drivers are " + (constructors[i][2]) + " and " + (constructors[i][3])) #print (constructors[0]) #print (constructors[0][3]) #print (constructors[0][2:4]) #for i in range (3): # print (constructors[i][2:4])
APPEND AND INSERT WITH 2D ARRAYS
By selecting position -1 you can append the last row within a 2D array. If you simply append the item will be placed outside of any inner arrays. If you want to add another array at the end of the array then you can use myList.append([]) note that you cannot append to apart of the array that does not exist so you may need to create the inner array/row first. Try the code below and you can see how the value of 135 is appended to the 2nd row in the list.
To insert a value into a 2D array you simply need to give the position where the value will go as can be seen in the code below.
2D ARRAY LENGTH
To find the total amount of elements in a 2D array
You could also try the short version with an inline for loop as can be seen below.
To find the total amount of internal arrays in a 2D array. The below method shows how to get the length of the internal array at index 1.
To find the total amount of elements in a specified internal array of a 2D array
Populating a list showing the length of each internal array.
append() Adds an element at the end of the list : append(item)
insert() Adds an element at the specified position : insert (position, item)
pop() Removes the element at the specified position : pop(index)
remove() Removes the item with the specified value : remove(item)
del Delete a range of items fro a list
reverse() Reverses the order of the list : myList.reverse()
index() Returns the index of the first element with the specified value : index(item)
sort() Sorts the list : myList.sort()
copy() Returns a copy of the list : newList = myList.copy()
join() Joins the list to be viewed as a single string : joinObject.join(myList)
clear() Removes all the elements from the list : listName.reverse()
set() Remove duplicates from a list: myList2 = set(myList1)
count() Returns the number of elements with the specified value : x = myList.count(item)
extend() Add the elements of a list (or any iterable), to the end of the current list : myList.extend(myOtherList)
if value in list Look to see if an item is in a list
if value not in list Look to see if a value is not in a list
insert() Adds an element at the specified position : insert (position, item)
pop() Removes the element at the specified position : pop(index)
remove() Removes the item with the specified value : remove(item)
del Delete a range of items fro a list
reverse() Reverses the order of the list : myList.reverse()
index() Returns the index of the first element with the specified value : index(item)
sort() Sorts the list : myList.sort()
copy() Returns a copy of the list : newList = myList.copy()
join() Joins the list to be viewed as a single string : joinObject.join(myList)
clear() Removes all the elements from the list : listName.reverse()
set() Remove duplicates from a list: myList2 = set(myList1)
count() Returns the number of elements with the specified value : x = myList.count(item)
extend() Add the elements of a list (or any iterable), to the end of the current list : myList.extend(myOtherList)
if value in list Look to see if an item is in a list
if value not in list Look to see if a value is not in a list
DESCRIPTION |
EXAMPLE |
|
append() |
Adds an element to the end of the list. |
fruits = ["apple", "banana"] fruits.append("cherry") print(fruits) # Outputs: ['apple', 'banana', 'cherry'] |
remove() |
Removes the first occurrence of the specified value from the list. |
fruits = ["apple", "banana", "cherry"] fruits.remove("banana") print(fruits) # Outputs: ['apple', 'cherry'] |
pop() |
Removes the element at the specified position, or the last element if no index is specified. |
fruits = ["apple", "banana", "cherry"] fruits.pop(1) print(fruits) # Outputs: ['apple', 'cherry'] |
extend() |
Adds elements from another list (or any iterable) to the end of the current list. |
fruits = ["apple", "banana"] fruits.extend(["cherry", "date"]) print(fruits) # Outputs: ['apple', 'banana', 'cherry', 'date'] |
count() |
Returns the number of times the specified value appears in the list. |
numbers = [1, 2, 3, 2, 4, 2] count = numbers.count(2) print(count) # Outputs: 3 |
index() |
Returns the index of the first occurrence of the specified value. |
fruits = ["apple", "banana", "cherry"] idx = fruits.index("banana") print(idx) # Outputs: 1 |
sort() |
Sorts the list in ascending order by default. |
numbers = [3, 1, 4, 2] numbers.sort() print(numbers) # Outputs: [1, 2, 3, 4] |
reverse() |
Reverses the order of the list. |
fruits = ["apple", "banana", "cherry"] fruits.reverse() print(fruits) # Outputs: ['cherry', 'banana', 'apple'] |
copy() |
Returns a copy of the list. |
newList = myList.copy() |
clear() |
Removes all the elements from the list |
listName.clear() |
set() |
Remove duplicates from a list |
myList2 = set(myList1) |
MOVIE LIST
starwars = ['IV', 'A New Hope', '1977','V', 'The Empire Strikes Back', '1980', 'VI', 'Return of the Jedi', '1983']
The list above contain the three original Starwars movie details.
Create a program that:
1: Outputs a list of the movie names
2: Asks the user which movie they would like to see the details of
3: Outputs the full details of the move
Example output
Please see movie list below:
1: A New Hope
2: The Empire Strikes Back
3: Return of the Jedi
Please select which movie you would like to see the detail, choose 1 - 3: 1
Details: IV A New Hope 1977
EXTENSION TASK
How could you make your program scalable? If the new Starwars movies were added to the list would your program still work without you making any changes?
The list above contain the three original Starwars movie details.
Create a program that:
1: Outputs a list of the movie names
2: Asks the user which movie they would like to see the details of
3: Outputs the full details of the move
Example output
Please see movie list below:
1: A New Hope
2: The Empire Strikes Back
3: Return of the Jedi
Please select which movie you would like to see the detail, choose 1 - 3: 1
Details: IV A New Hope 1977
EXTENSION TASK
How could you make your program scalable? If the new Starwars movies were added to the list would your program still work without you making any changes?
Question 1: 1D Lists
How do you append an item to the end of a list in Python?
A: list.add(item)
B: list.append(item)
C: list.insert(item)
D: list.extend(item)
EXPLANATION
Correct Answer: B) list.append(item)
Explanation: The append() method is used to add an item to the end of a list in Python. The other options are either incorrect or used for different purposes
Explanation: The append() method is used to add an item to the end of a list in Python. The other options are either incorrect or used for different purposes
Question 2: Accessing List Elements
Given the list `numbers = [10, 20, 30, 40]`, what will `numbers[2]` return?
A: 10
B: 20
C: 30
D: 40
EXPLANATION
Correct Answer: C) 30
Explanation: List indices start at 0 in Python, so numbers[2] refers to the third element, which is 30.
Explanation: List indices start at 0 in Python, so numbers[2] refers to the third element, which is 30.
Question 3: 2D Lists
How can you access the element `7` from the following 2D list?
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
A: matrix[3][1]
B: matrix[2][1]
C: matrix[2][0]
D: matrix[1][2]
EXPLANATION
Correct Answer: C) matrix[2][0]
Explanation: In a 2D list, the first index refers to the row, and the second index refers to the column. matrix[2][0] accesses the third row, first element, which is 7.
Explanation: In a 2D list, the first index refers to the row, and the second index refers to the column. matrix[2][0] accesses the third row, first element, which is 7.
Question 4: Tuples
Which of the following is TRUE about tuples in Python?
A: Tuples are mutable (can be changed after creation).
B: Tuples can contain duplicate values.
C: You cannot access elements in a tuple.
D: Tuples must only contain integers.
EXPLANATION
Correct Answer: B) Tuples can contain duplicate values.
Explanation: Tuples are immutable, meaning they cannot be changed after creation, but they can contain duplicate values. Elements in a tuple can be accessed by index, and a tuple can contain any data type.
Explanation: Tuples are immutable, meaning they cannot be changed after creation, but they can contain duplicate values. Elements in a tuple can be accessed by index, and a tuple can contain any data type.
Question 5: Dictionary Keys
Which of the following is NOT allowed as a key in a Python dictionary?
A: An integer
B: A string
C: A tuple
D: A list
EXPLANATION
Correct Answer: D) A list
Explanation: Dictionary keys must be immutable, and since lists are mutable, they cannot be used as dictionary keys. Integers, strings, and tuples are immutable and can be used as keys.
Explanation: Dictionary keys must be immutable, and since lists are mutable, they cannot be used as dictionary keys. Integers, strings, and tuples are immutable and can be used as keys.
Question 6: Dictionary Access
Given the dictionary `student = {"name": "John", "age": 21, "grade": "A"}`, what will `student["age"]` return?
A: "John"
B: 21
C: "A"
D: None
EXPLANATION
Correct Answer: B) 21
Explanation: To access the value associated with a key in a dictionary, you use square brackets. student["age"] will return the value 21.
Explanation: To access the value associated with a key in a dictionary, you use square brackets. student["age"] will return the value 21.
Question 7: Modifying a List
Given the list `fruits = ["apple", "banana", "cherry"]`, what does `fruits[1] = "orange"` do?
A: Replaces "apple" with "orange"
B: Replaces "banana" with "orange"
C: Replaces "cherry" with "orange"
D: Throws an error
EXPLANATION
Correct Answer: B) Replaces "banana" with "orange"
Explanation: Since list indices start at 0, fruits[1] refers to "banana". This line replaces "banana" with "orange".
Explanation: Since list indices start at 0, fruits[1] refers to "banana". This line replaces "banana" with "orange".
Question 8: Iterating Through a Dictionary
How can you iterate through all the keys of a dictionary `my_dict`?
A: for key in my_dict:
B: for key in my_dict.keys():
C: for key in my_dict.items():
D: Both A and B
EXPLANATION
Correct Answer: D) Both A and B
Explanation: You can iterate through all the keys of a dictionary using either for key in my_dict: or for key in my_dict.keys():. Both will give you access to the dictionary's keys.
Explanation: You can iterate through all the keys of a dictionary using either for key in my_dict: or for key in my_dict.keys():. Both will give you access to the dictionary's keys.
SECTION 4 | TUPLES
Turples are very much like lists except after creation you cannot edit them, they are immutable. With lists square brackets are used with tuples parentheses ( ) are used. As with lists elements of the turple can be accessed individually.
studentNames = ("Dave", "Jane", "Bill" ,"Bob" , "Jack") print (studentNames[2])
Copy, paste and try the code above. You will see that turple will print 'Bill'. Turples are very useful if you do not need the content of the turple to change.
SECTION 5 | CHANGE A TUPLE TO A LIST
In the example below the tuple have been generated from fetching data from a backend database, it is not in a nice format as it have an extra comma after each value and is a series of tuples within a list as can be seen below.
myData = [("Tetris",),("Pacman",),("Space Invaders",),("Astroids",)]
Each tuple has a list index, for example 'Pacman' is in list index 1, however 'Pacmac' is also in index 0 of the inner tuple, therefore to refer to the value 'Pacman' you would need to stipulate both index: myData[1][0]
An example of changing this tuple to a normal single dimensional list can be seen below.
myData = [("Tetris",),("Pacman",),("Space Invaders",),("Astroids",)]
Each tuple has a list index, for example 'Pacman' is in list index 1, however 'Pacmac' is also in index 0 of the inner tuple, therefore to refer to the value 'Pacman' you would need to stipulate both index: myData[1][0]
An example of changing this tuple to a normal single dimensional list can be seen below.
myList = [] myData = [("Tetris",),("Pacman",),("Space Invaders",),("Astroids",)] for i in range (len(myData)): myList.append(myData[i][0]) print (myList)
SECTION 6 | DICTIONARIES
A dictionary is used to store data just like a list except a dictionary has key and a value. For example a student name could be the key and the student grade could be the value. Dictionaries are created by using curly brackets and each pair of key with value is separated with a comma. Below is an example of a dictionary containing student names as a key and grade as a value.
studentNames = {"Dave": 6, "Jane" : 9, "Bill" : 4 ,"Bob" : 6 , "Jack" : 5}
print (studentNames["Jane"])
Copy, paste and try the code above. The print is '9', because 9 is the value from the key Jane.
print (studentNames["Jane"])
Copy, paste and try the code above. The print is '9', because 9 is the value from the key Jane.