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:
Like 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.
arr = ["a", "b", "c", "d"]
arr[0] #> "a"
arr[1] #> "b"
arr[2] #> "c"
arr[3] #> "d"
arr[4] #> IndexError: list index out of range
arr.index("a") #> 0
arr.index("b") #> 1
arr.index("c") #> 2
arr.index("z") #> -1 (applies to any item not found in the list)
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":
A list can be iterated, or "looped" using a for ... in ... statement:
for letter in ["a", "b", "c", "d"]:
print(letter)
#> a
#> b
#> c
#> d
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:
arr = [1, 2, 3, 4]
arr2 = []
for i in arr:
arr2.append(i * 100)
arr #> [1, 2, 3, 4]
arr2 #> [100, 200, 300, 400]
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:
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:
arr = [1,2,4,8,16]
def all_of_them(i):
return True # same as ... return i == i
def equals_two(i):
return i == 2
def greater_than_two(i):
return i > 2
def really_big(i):
return i > 102
filter(all_of_them, arr) #> <filter at 0x103fa71d0>
list(filter(all_of_them, arr)) #> [1, 2, 4, 8, 16]
list(filter(equals_two, arr)) #> [2]
list(filter(greater_than_two, arr)) #> [4, 8, 16]
list(filter(really_big, arr)) #> []
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:
teams = [
{"city": "New York", "name": "Yankees"},
{"city": "New York", "name": "Mets"},
{"city": "Boston", "name": "Red Sox"},
{"city": "New Haven", "name": "Ravens"}
]
def yanks(obj):
return obj["name"] == "Yankees"
def from_new_york(obj):
return obj["city"] == "New York"
def from_new_haven(obj):
return obj["city"] == "New Haven"
def from_new_something(obj):
return "New" in obj["city"]
list(filter(yanks, teams)) #> [{...}]
list(filter(from_new_york, teams)) #> [{...}, {...}]
list(filter(from_new_haven, teams)) #> [{...}]
list(filter(from_new_something, teams)) #> [{...}, {...}, {...}]
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:
teams = [
{"city": "New York", "name": "Yankees"},
{"city": "New York", "name": "Mets"},
{"city": "Boston", "name": "Red Sox"},
{"city": "New Haven", "name": "Ravens"}
]
# using a list comprehension
def teams_from(city):
return [team for team in teams if team["city"] == city]
# using "lambda" syntax
def teams_from2(city):
return list(filter(lambda team: team["city"] == city, teams))
# the long way
def teams_from3(city):
matches = []
for team in teams:
if team["city"].upper() == city.upper():
matches.append(team)
return matches
print(teams_from("New York")) #> [{'city': 'New York', 'name': 'Yankees'}, {'city': 'New York', 'name': 'Mets'}]
print(teams_from2("New York")) #> [{'city': 'New York', 'name': 'Yankees'}, {'city': 'New York', 'name': 'Mets'}]
print(teams_from3("New York")) #> [{'city': 'New York', 'name': 'Yankees'}, {'city': 'New York', 'name': 'Mets'}]