Fall 2024 - P1
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
Big Idea 3.10 Part 1- Creating Lists
Learn more about lists and how to create them!
Lists
In Python, a list is an ordered, mutable collection that can hold different types of data. This makes lists versatile, as they can store integers, strings, or even other lists. One key feature of lists in Python is that they are indexed starting from 0, meaning the first element in a list is at index 0. For example, in the list my_list = ['apple', 'banana', 'cherry']
, the first element is 'apple'
, located at my_list[0]
.
Learning Objectives,
- You will be able to write expressions that use list indexing and list procedures.
- For algorithms involving elements of a list, you will be able to write iteration statements to traverse a list.
Key Points
- Ordered: The elements have a defined order, meaning you can refer to an item by its index.
- Mutable: You can change the items within a list (unlike tuples which are immutable).
List Operations
Python provides several operations to interact with lists, allowing you to access, modify, and remove elements.
- aList[i]: Accesses the element at index i in the list. Index is a numeric value which tells you where an element is within the data. The first item is at index 1, so aList[1].
- x <- aList[i]: Assigns the value of aList[i] to the variable x.
- aList[i] <- x: Assigns the value of x to aList[i].
- aList[i] <- aList[j]: Assigns value of aList[j] to aList[i].
- INSERT(aList, i, value): Inserts the value into aList at index i.
- APPEND(aList, value): Adds value to the end of the list.
- REMOVE(aList, i): Removes the item at index i from aList.
- LENGTH(aList): Evaluates the number of elements in aList.
Here’s an example of these operations in action aList = [1, 2, 3, 4] aList[2] = 5 # Changes the 3rd element from 3 to 5 aList.append(6) # Adds 6 at the end aList.insert(1, 10) # Inserts 10 at index 1 print(aList) # Output: [1, 10, 2, 5, 4, 6]
Creating a List
# Python
# Creating an empty list
aList = []
# Creating a list with elements
aList = [1, 2, 3, 'apple', 'banana']
%%js
// Javascript
// Creating an empty array
let aList = [];
// Creating an array with elements
aList = [1, 2, 3, 'apple', 'banana'];
Accessing Elements
You can access an element at a specific index using the syntax aList[i]
. Remember, the first element is at index 0
.
# Python
aList = ['apple', 'banana', 'cherry']
print(aList[0]) # Output: 'apple'
%%js
// Javascript
const aList = ['apple', 'banana', 'cherry'];
const firstElement = aList[0]; // apple
const secondElement = aList[1]; // banana
Appending Elements
Use the append()
method to add an element to the end of the list.
# Python
aList.append('grape') # Adds 'grape' at the end
print(aList) # Output: ['apple', 'banana', 'cherry', 'kiwi', 'grape']
%%js
// Javascript
let aList = ['apple', 'banana', 'cherry', 'kiwi'];
// Adding 'grape' at the end
aList.push('grape');
// Printing the array
console.log(aList); // Output: ['apple', 'banana', 'cherry', 'kiwi', 'grape']
Popcorn Hack
- Try making your own list then apend an element