Learn Python 3 - Lists Cheatsheet - Codecademy
Learn Python 3 - Lists Cheatsheet - Codecademy
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 empty_list = []
values in a list do not need to be unique (the same value
can be repeated).
Empty lists do not contain any values within the square
brackets.
/
Aggregating Iterables Using zip()
In Python, data types that can be iterated (called
iterables) can be used with the zip() function to owners_names = ['Jenny', 'Sam', 'Alexis']
aggregate data. The zip() function takes iterables, dogs_names = ['Elphonse', 'Dr. Doggy DDS',
aggregates corresponding elements based on the 'Carter']
iterables passed in, and returns an iterator. Each element owners_dogs = zip(owners_names,
of the returned iterator is a tuple of values. dogs_names)
As shown in the example, zip() is aggregating the print(list(owners_dogs))
data between the owners’ names and the dogs’ names # Result: [('Jenny', 'Elphonse'), ('Sam',
to match the owner to their dogs. zip() returns an 'Dr.Doggy DDS'), ('Alexis', 'Carter')]
iterator containing the data based on what the user
passes to the function. Empty iterables passed in will
result in an empty iterator. To view the contents of the
iterator returned from zip() , we can cast it as a list
by using the list() function and printing the results.
/
Zero-Indexing
In Python, list index begins at zero and ends at the
length of the list minus one. For example, in this list, names = ['Roger', 'Rafael', 'Andy',
'Andy' is found at index 2 . 'Novak']
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"
List Slicing
A slice, or sub-list of Python list elements can be
selected from a list using a colon-separated starting and tools = ['pen', 'hammer', 'lever']
ending point. tools_slice = tools[1:3] # ['hammer',
The syntax pattern is 'lever']
myList[START_NUMBER:END_NUMBER] . The tools_slice[0] = 'nail'
slice will include the START_NUMBER index, and
everything until but excluding the END_NUMBER item. # Original list is unaltered:
When slicing a list, a new list is returned, so if the slice is print(tools) # ['pen', 'hammer', 'lever']
saved and then altered, the original list remains the
same.
/
sorted() Function
The Python sorted() function accepts a list as an
argument, and will return a new, sorted list containing unsortedList = [4, 2, 1, 3]
the 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.