pytutorial/python_basics/list
David Rotermund bf37d9cf52
Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-11 12:42:53 +01:00
..
README.md Update README.md 2023-12-11 12:42:53 +01:00

Sequence Types: list

{:.no_toc}

* TOC {:toc}

The goal

Lists are mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application).

Quote

Questions to David Rotermund

Example Lists

With homogeneous items:

primes = [2, 3, 5, 7]

collection_of_strings = [
    "AA",
    "BB",
    "CC",
    "DD",
    "EE",
    "FF",
    "GG",
    "HH",
]

A mixture of type:

def my_function(a):
    return a

my_favourite_things = [32, "sleep", my_function]

Indexing

collection_of_strings = [
    "AA",
    "BB",
    "CC",
    "DD",
    "EE",
    "FF",
    "GG",
    "HH",
]


print(collection_of_strings[0])  # -> AA
print(collection_of_strings[1])  # -> BB
print(collection_of_strings[-2])  # -> GG
print(collection_of_strings[-1])  # -> HH

Slicing

collection_of_strings = [
    "AA",
    "BB",
    "CC",
    "DD",
    "EE",
    "FF",
    "GG",
    "HH",
]


print(collection_of_strings[0:3])  # -> ['AA', 'BB', 'CC']
print(collection_of_strings[:3])  # -> ['AA', 'BB', 'CC']
print(collection_of_strings[3:])  # -> ['DD', 'EE', 'FF', 'GG', 'HH']
print(collection_of_strings[1:-1])  # -> ['BB', 'CC', 'DD', 'EE', 'FF', 'GG']
print(collection_of_strings[-3:])  # -> ['FF', 'GG', 'HH']

Changing lists

collection_of_strings = [
    "AA",
    "BB",
    "CC",
    "DD",
    "EE",
    "FF",
    "GG",
    "HH",
]

collection_of_strings[3] = "II"
print(collection_of_strings)  # -> ['AA', 'BB', 'CC', 'II', 'EE', 'FF', 'GG', 'HH']
collection_of_strings[:3] = ["JJ", "KK", "LL"]
print(collection_of_strings)  # -> ['JJ', 'KK', 'LL', 'II', 'EE', 'FF', 'GG', 'HH']
collection_of_strings[:4] = [
    "MM",
    "NN",
    "OO",
    "PP",
]
print(collection_of_strings)  # -> ['MM', 'NN', 'OO', 'PP', 'EE', 'FF', 'GG', 'HH']

List functions

len() and sorted()

collection_of_strings = [
    "GG",
    "HH",
    "AA",
    "BB",
    "EE",
    "FF",
    "CC",
    "DD",
]


print(len(collection_of_strings))  # -> 8
print(sorted(collection_of_strings)) # -> ['AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG', 'HH']

max() and sum()

primes = [2, 3, 5, 7]
print(sum(primes)) # -> 17
print(max(primes)) # -> 7

append(), pop(), and remove()

collection_of_strings = [
    "AA",
    "BB",
    "BB",
    "CC",
    "DD",
    "EE",
    "FF",
    "GG",
    "HH",
]

collection_of_strings.append("II")
print(
    collection_of_strings
)  # -> ['AA', 'BB', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG', 'HH', 'II']
print(collection_of_strings.pop())  # -> II
print(
    collection_of_strings
)  # -> ['AA', 'BB', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG', 'HH']

collection_of_strings.remove("BB")
print(collection_of_strings)  # -> ['AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG', 'HH']

collection_of_strings.remove("BB")
print(collection_of_strings)  # -> ['AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG', 'HH']

collection_of_strings.remove("BB")
print(collection_of_strings)  # -> ValueError: list.remove(x): x not in list

Index

collection_of_strings = [
    "AA",
    "BB",
    "CC",
    "DD",
    "EE",
    "FF",
    "GG",
    "HH",
]

print(collection_of_strings.index("CC"))  # -> 2
print(collection_of_strings.index("XX"))  # -> ValueError: 'XX' is not in list

del and insert

collection_of_strings = []
collection_of_strings.append("BB")
collection_of_strings.append("CC")
collection_of_strings.append("DD")
print(collection_of_strings)  # -> ['BB', 'CC', 'DD']

collection_of_strings.insert(0, "AA")
print(collection_of_strings)  # -> ['AA', 'BB', 'CC', 'DD']

del collection_of_strings[1]
print(collection_of_strings)  # -> ['AA', 'CC', 'DD']