Recently, I have learned a bit about the filter method, an alternative to using list comprehensions.
Say I have a list as such:
names = ["Bob", "Billy", "Samuel", "Adam", "Rob"]
Now, I would like to get a list containing the names that start with the letter, "B". I could go about this in a couple of ways. This is one:
b_starting_names = list(filter(lambda name: name.startswith("B"), names))
This is another:
b_starting_names = [name for name in names if name.startswith("B")]
Could someone please explain what the difference is between a list comprehension and the filter function, and why someone may want to use one over the other?