Python Cheat Sheet 3
Python Cheat Sheet 3
Lists
Lists
In Python, lists are ordered collections of items that allow
for easy use of a set of data. primes = [2, 3, 5, 7, 11]
List values are placed in between square brackets [ ] , print(primes)
separated by commas. It is good practice to put a space
between the comma and the next value. The values in a empty_list = []
list do not need to be unique (the same value can be
repeated).
Empty lists do not contain any values within the square
brackets.
Zero-Indexing
In Python, list index begins at zero and ends at the length
of the list minus one. For example, in this list, 'Andy' is names = ['Roger', 'Rafael', 'Andy',
found at index 2 . 'Novak']
1 of 5 28/12/2021, 2:04 pm
Firefox https://www.codecademy.com/learn/learn-python-3/modules/learn-pytho...
List Indices
Python list elements are ordered by index, a number
referring to their placement in the list. List indices start at berries = ["blueberry", "cranberry",
0 and increment by one. "raspberry"]
To access a list element by index, square bracket notation
is used: list[index] . berries[0] # "blueberry"
berries[2] # "raspberry"
Modifying 2D Lists
In order to modify elements in a 2D list, an index for the
sublist and the index for the element of the sublist need # A 2D list of names and hobbies
to be provided. The format for this is class_name_hobbies = [["Jenny",
list[sublist_index][element_in_sublist_index] =
"Breakdancing"], ["Alexus",
new_value .
"Photography"], ["Grace", "Soccer"]]
# Output
# [["Jenny", "Meditation"], ["Alexus",
"Photography"], ["Grace", "Soccer"]]
2 of 5 28/12/2021, 2:04 pm
Firefox https://www.codecademy.com/learn/learn-python-3/modules/learn-pytho...
Accessing 2D Lists
In order to access elements in a 2D list, an index for the
sublist and the index for the element of the sublist both # 2D list of people's heights
need to be provided. The format for this is heights = [["Noelle", 61], ["Ali", 70],
list[sublist_index][element_in_sublist_index] . ["Sam", 67]]
# Access the sublist at index 0, and then
access the 1st index of that sublist.
noelles_height = heights[0][1]
print(noelles_height)
# Output
# 61
# Output
# ["Cole", "Kip", "Sylvana", "Chris"]
print(numPen)
# Output: 3
3 of 5 28/12/2021, 2:04 pm
Firefox https://www.codecademy.com/learn/learn-python-3/modules/learn-pytho...
List Slicing
A slice, or sub-list of Python list elements can be selected
from a list using a colon-separated starting and ending tools = ['pen', 'hammer', 'lever']
point. tools_slice = tools[1:3] # ['hammer',
The syntax pattern is myList[START_NUMBER:END_NUMBER] . 'lever']
The slice will include the START_NUMBER index, and tools_slice[0] = 'nail'
everything until but excluding the END_NUMBER item.
When slicing a list, a new list is returned, so if the slice is
# Original list is unaltered:
saved and then altered, the original list remains the same.
print(tools) # ['pen', 'hammer', 'lever']
sorted() Function
The Python sorted() function accepts a list as an
argument, and will return a new, sorted list containing the unsortedList = [4, 2, 1, 3]
same elements as the original. Numerical lists will be sortedList = sorted(unsortedList)
sorted in ascending order, and lists of Strings will be print(sortedList)
sorted into alphabetical order. It does not modify the # Output: [1, 2, 3, 4]
original, unsorted list.
print(store_line)
# Output: ['Karla', 'Maxium', 'Vikor',
'Martim', 'Isabella']
4 of 5 28/12/2021, 2:04 pm
Firefox https://www.codecademy.com/learn/learn-python-3/modules/learn-pytho...
print(cs_topics)
print(removed_element)
# Output:
# ['Python', 'Data Structures', 'Balloon
Making', 'Algorithms']
# 'Clowns 101'
# Output:
# ['Python', 'Data Structures',
'Algorithms']
5 of 5 28/12/2021, 2:04 pm