Week 8 - Lists
Lists in Python¶
Lists are similar to Arrays in other languages, but the are dynamic due to the dynamic nature of a Python. We just learned about string sequences, and guess what, lists are also sequences and many of the same operations we could use with strings we can use with lists.
1. Overview of Lists¶
- Definition: A list is a sequence that can store values of any data type, unlike strings that only store characters.
- Creating Lists: Show examples of lists containing integers, strings, and mixed types.
1 2
fruits = ["apple", "banana", "cherry"] mixed_list = ["Python", 3.14, 42, ["nested", "list"]]
- Empty Lists: Mention that lists can also be empty, which can be useful as placeholders.
1
empty_list = []
2. Accessing and Modifying List Elements¶
- Indexing: Explain list indexing with examples for positive and negative indices.
1 2 3
items = [10, 20, 30, 40] print(items[0]) # First item print(items[-1]) # Last item
- Mutability: Lists are mutable, so elements can be reassigned.
1 2
items[1] = 25 print(items) # Output: [10, 25, 30, 40]
- Checking Elements with
in
: Introduce thein
operator.1 2
print(30 in items) # True print(50 in items) # False
3. List Slicing¶
- Using Slice Syntax: Demonstrate slicing to access sublists.
1 2
letters = ['a', 'b', 'c', 'd', 'e'] print(letters[1:4]) # ['b', 'c', 'd']
- Omitting Indices: Show slicing from the start or to the end of the list.
1 2
print(letters[:3]) # ['a', 'b', 'c'] print(letters[3:]) # ['d', 'e']
- Copying Lists: Explain that omitting both indices creates a copy of the list.
1
letters_copy = letters[:]
4. List Operations¶
- Concatenation with
+
: Combine two lists using the+
operator.1 2 3
list1 = [1, 2, 3] list2 = [4, 5, 6] combined = list1 + list2
- Repetition with
*
: Use*
to repeat elements in a list.1 2
repeated_list = ["hi"] * 3 # Output: ['hi', 'hi', 'hi']
- Basic Aggregations: Introduce
sum
,min
, andmax
.1 2
numbers = [10, 20, 30] print(sum(numbers)) # 60
5. List Methods¶
- Adding Elements:
append()
: Adds a single element at the end.1 2
letters.append('f') # Output: ['a', 'b', 'c', 'd', 'e', 'f']
extend()
: Adds multiple elements.1 2
letters.extend(['g', 'h']) # Output: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
- Removing Elements:
pop()
: Removes by index.1
letters.pop(2) # Removes 'c'
remove()
: Removes by value.1
letters.remove('a')
6. Working with Lists and Strings¶
- Converting a String to a List of Characters:
1 2
name = "python" name_list = list(name)
- Splitting a String into Words:
1 2
sentence = "Learning Python is fun" words = sentence.split()
- Joining List Elements into a String:
1
sentence = ' '.join(words)
7. Looping Through Lists¶
- Using
for
Loops: Introduce iterating over a list.1 2 3
numbers = [1, 2, 3] for number in numbers: print(number)
- Looping with Index:
1 2
for i in range(len(numbers)): print(f"Index {i}: {numbers[i]}")
8. Sorting Lists¶
sorted()
Function: Demonstrate sorting a list and that it does not modify the original.1 2
unsorted_list = [3, 1, 2] sorted_list = sorted(unsorted_list)
- Using
join
withsorted()
: Combine sorted list items back into a string.1 2
letters = ['d', 'a', 'c'] sorted_str = ''.join(sorted(letters))
Exercises¶
- Combine and Modify
-
Write a function that takes two lists, combines them, and then removes any duplicates. The function should return the modified list.
-
List Reversal Without
reverse()
- Write a function that takes a list and returns a new list with the elements in reverse order without using the
reverse()
method or slicing. Instead, use a loop to achieve the reversal.