Lists
Reference:
A List represents a numbered, ordered collection of items. A List may contain zero or more items. A list can contain items of any datatype, but as a best practice, all items in a list should share a datatype and structure:
# DO:
[]
[1, 2, 3, 4]
[100, 75, 33]
["fun", "times", "right?"]
[{"a": 1, "b": 2}, {"a": 5, "b": 6}] # lists can contain dictionaries
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] # lists can be "nested" inside other lists
# DON'T:
[100, "fun"] # mixed datatypes
[{"a": 1, "b": 2}, {"x": 5, "z": 6}] # non-standard dictionary keysLike other languages, individual list items can be accessed by their index. List item indices are zero-based, meaning the index of the first list item is 0.
Equality operators apply:
Inclusion operators apply:
Common list functions and operators include the following built-in Python functions:
Add an element to the end of a list:
Remove an element from a list by specifying the index of the item you would like to remove:
Concatenate two lists:
Remove duplicate values in a list by converting it to another datatype called a "Set" (which rejects non-unique values), and then converting it back to a "List":
Sorting
Sort a list:
If you have a list of dictionaries, you should be able to sort it based on dictionary values:
Alternatively for simple attribute-based sorting, you could use the operator module's itemgetter() function, for example:
Iteration
Reference:
A list can be iterated, or "looped" using a for ... in ... statement:
TIP: If it helps, you can vocalize this like "for each item in the list of items, do something with that item"
A common pattern is to loop through one list to populate the contents of another:
Mapping
Lists can be looped "in-place" using Python's built-in map() function. The map() function takes two parameters. The first parameter is the name of a pre-defined function to perform on each item in the list. The function should accept a single parameter representing a single list item. The second parameter is the actual list to be operated on:
NOTE: remember to use the
returnkeyword in your mapping function!
Another way of mapping is to use a list comprehension:
Filtering
Reference: https://docs.python.org/3/library/functions.html#filter.
Use the filter() function to select a subset of items from a list - only those items matching a given condition. The filter function accepts the same parameters as the map() fuction:
Note: depending on how many items matched the filter condition, the resulting filtered list may be empty, or it may contain one item, or it may contain multiple items
When using the filter function, observe this alternative filtering syntax involving the keyword lambda:
If your list is full of dictionaries, you can filter() based on their attribute values:
If you need to implement complex filtering conditions, consider using a list comprehension, or "lambda" syntax, or consider writing out your function the long way:
Grouping
Reference the itertools module for additional operations.
Last updated
Was this helpful?