BackProgramming with Lists and Tuples in Python
Study Guide - Smart Notes
Tailored notes based on your materials, expanded with key definitions, examples, and context.
Programming with Lists and Tuples
The list Object
In Python, a list is an ordered sequence of elements that can store items of any type. Lists are denoted by square brackets [ ] and items are separated by commas. Importantly, lists are mutable, meaning their contents can be changed after creation.
Definition: A list is a collection of items, which can be of mixed types, stored in a specific order.
Syntax: list_name = [item1, item2, item3]
Example: team = ["Seahawks", 2014, "CenturyLink Field"]
Mutability: Lists can be modified after creation (items can be added, removed, or changed).
Creating Lists from Strings
The list() function can convert a string into a list of its characters. However, it cannot convert an integer directly into a list, as integers are not iterable.
Example: phoneNumber = "9876543219" c1 = list(phoneNumber) Output: ['9', '8', '7', '6', '5', '4', '3', '2', '1', '9']
Error Example: phoneNumber2 = 9876543219 c2 = list(phoneNumber2) Output: TypeError: 'int' object is not iterable
Indexing in Lists
Items in a list are indexed starting from 0 for the first element. Negative indices start from -1 for the last element and count backwards.
Accessing Elements: listName[i] returns the item at index i.
Example: team[1] = 2014, words[-2] = "spam"
Practice: What is team[i-1]? What is nums[0]+1? What is words[2]?
List Operations and Methods
Python provides various built-in functions and methods to manipulate lists. These include operations for measuring, modifying, and accessing list contents.
Function or Method | Example | Value | Description |
|---|---|---|---|
len(words) | len(words) | 2 | Number of items in list |
max(nums) | max(nums) | 10 | Greatest item (items must be same type) |
min(nums) | min(nums) | 4 | Least item (items must be same type) |
sum(nums) | sum(nums) | 24 | Total (items must be numbers) |
nums.count(5) | nums.count(5) | 2 | Number of occurrences of an item |
nums.index(4) | nums.index(4) | 2 | Index of first occurrence of an item |
words.reverse() | words.reverse() | ['ni', 'spam'] | Reverses the order of the items |
team.clear() | team.clear() | [] | Empties the list |
Method | Example | Value | Description |
|---|---|---|---|
nums.append(7) | nums.append(7) | [5, 10, 4, 5, 7] | Inserts object at end of list |
nums.extend([1, 2]) | nums.extend([1, 2]) | [5, 10, 4, 5, 1, 2] | Inserts new list's items at end |
del team[1] | del team[1] | ['Seahawks', 'CenturyLink Field'] | Removes item with stated index |
nums.remove(5) | nums.remove(5) | [10, 4, 5] | Removes first occurrence of an object |
words.insert(1, "wink") | words.insert(1, "wink") | ['spam', 'wink', 'ni'] | Insert new item before item at index |
[a] + [b] | [a] + [b] | [a, b] | Concatenation; same as extend([b]) |
[0] * 3 | [0] * 3 | [0, 0, 0] | List repetition |
Practice: Modifying Lists
Lists can be modified by inserting, deleting, or updating elements. For example, given L = ["sentence", "contains", "five", "words"]:
Insert "This" as the first element: L.insert(0, "This")
Display list L: print(L)
Delete 4th element: del L[3]
Insert "six" as the 4th element: L.insert(3, "six")
Insert "different" as the 5th element: L.insert(4, "different")
Display list L: print(L)
Practice: Calculating Average After Dropping Lowest Grades
Write a program that requests five grades as input, drops the two lowest grades, and calculates the average of the remaining three.
Algorithm:
Input five grades and store in a list.
Sort the list and remove the two lowest grades.
Calculate the average of the remaining grades.
Display the result.
Example Output:
a grade: 40
a grade: 35
a grade: 14
a grade: 98
a grade: 100
Average is 79.33
*Additional info: More advanced list operations include slicing, copying, and using built-in methods for efficient data manipulation. Lists are foundational for data structures and algorithms in Python.*