Using lists in Python makes life easier. It saves us from having to call a function for multiple items, we can just batch them together so it only has to be called once. Though this only works if each item is being called the same way.
We can check if an item is in a list with the ‘in’ keyword, that returns a Boolean (True or False) result.
def test(x):
numbers = [11, 22, 33, 44]
return x in numbers
I didn’t really get this one because I thought we were checking if an actual item is in the list. I’ll figure it out later.
Here’s a code if I wanted a Boolean return on whether a list called ‘numbers’ has a length of 3 or less
def is_short(numbers):
if len(numbers) <= 3:
return True
else:
return False
We’re coming back to the len() function
For combining lists, it’s done the same way as any other concatenation, with the plus sign. If you’re calling a parameter to combine a list, make sure you’re setting a ‘result’ behind it and returning that result.
In Python, or I think programming in general, we count from 0. So the first character (or ‘index’ is the more formal term) will be 0.
If I want to extract a value from a list:
xs = [7, 8, 9]
x = xs[1]
print(x)
Output: 8
Mind you the 1 in the second line is asking for the second index in that list, which is 8.
You can change an item in a list
colors = ["red", "green", "blue"]
colors[1] = "purple"
print(colors)
Output: ['red', 'purple', 'blue']
I’ll admit I had to glaze over a lot of the challenges for this. This’ll be one of the main things I have to go back over