Introduction to List

The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so forth.

Python has six built-in types of sequences, but the most common ones are lists and tuples, which we would see in this tutorial.

There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.

Method 1: map()

# Create a list of casualties from battles
battleDeaths = [482, 93, 392, 920, 813, 199, 374, 237, 244]
# Create a function that updates all battle deaths by adding 100
def updated(x): return x + 100
# Create a list that applies updated() to all elements of battleDeaths
list(map(updated, battleDeaths))

#output
[582, 193, 492, 1020, 913, 299, 474, 337, 344]

Method 2: for x in y

# Create a list of deaths
casualties = [482, 93, 392, 920, 813, 199, 374, 237, 244]
# Create a variable where we will put the updated casualty numbers
casualtiesUpdated = []
# Create a function that for each item in casualties, adds 10
for x in casualties:
    casualtiesUpdated.append(x + 100)
# View casualties variables
casualtiesUpdated

# Output
[582, 193, 492, 1020, 913, 299, 474, 337, 344]

Method 3: lambda functions

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.

# Map the lambda function x() over casualties
list(map((lambda x: x + 100), casualties))

#output
[582, 193, 492, 1020, 913, 299, 474, 337, 344]