regimentSize = (5345, 6436, 3453, 2352, 5212, 6232, 2124, 3425, 1200, 1000, 1211); regimentSize
(5345, 6436, 3453, 2352, 5212, 6232, 2124, 3425, 1200, 1000, 1211)
This line of code does the same thing as the multiline method below, it is just more compact (but also more complicated to understand.
smallRegiments = list(filter((lambda x: x < 2500), regimentSize)); smallRegiments
[2352, 2124, 1200, 1000, 1211]
The ease with interpreting what is happening, I've broken down the one-line filter method into multiple steps, one per line of code. This appears below.
lessThan2500Filter = lambda x: x < 2500
filteredRegiments = filter(lessThan2500Filter, regimentSize)
smallRegiments = list(filteredRegiments)
[2352, 2124, 1200, 1000, 1211]
This for loop does the same as both methods above, except it uses a for loop.
# Create a variable for the results of the loop to be placed
smallRegiments_2 = []
# for each item in regimentSize,
for x in regimentSize:
# look if the item's value is less than 2500
if x < 2500:
# if true, add that item to smallRegiments_2
smallRegiments_2.append(x)
smallRegiments_2
[2352, 2124, 1200, 1000, 1211]