From bf37d9cf52c9ff1dcba7d3b027cc55405624c594 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Mon, 11 Dec 2023 12:42:53 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- python_basics/list/README.md | 42 +++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/python_basics/list/README.md b/python_basics/list/README.md index 5f5ef15..2c998b4 100644 --- a/python_basics/list/README.md +++ b/python_basics/list/README.md @@ -142,7 +142,7 @@ print(sum(primes)) # -> 17 print(max(primes)) # -> 7 ``` -### [append()](), [pop()](), and [remove()]() +### [append()](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists), [pop()](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists), and [remove()](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists) ```python collection_of_strings = [ @@ -165,7 +165,47 @@ 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](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists) + +```python +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](https://docs.python.org/3/tutorial/datastructures.html#the-del-statement) and [insert](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists) + +```python +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'] +```