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
- Creating a List
- Accessing Elements
- Appending Elements
- Here’s one more example of these operations in Python
- Popcorn Hack
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).
Creating a List
This code creates a list and assigns it to the variable aList
. You can use this empty list to add elements later in your code.
# Python
# Creating an empty list
aList = []
%%js
// Javascript
// Creating an empty array
let aList = [];
This code reassigns the variable aList to a new list that contains five elements: the integers 1, 2, 3, and the strings ‘apple’ and ‘banana’.
# Python
# Creating a list with elements
aList = [1, 2, 3, 'apple', 'banana']
%%js
// Javascript
// Creating an array with elements
let 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'
These lines access the first and second elements of the list “aList” using the index 0 and assigns it to the constant firstElement and secondElement.
%%js
// Javascript
const aList = ['apple', 'banana', 'cherry'];
const firstElement = aList[0]; // apple
const secondElement = aList[1]; // banana
Appending Elements
In Python, use the append()
method to add an element to the end of the list. This method is a built-in function in Python that modifies the original list without needing to create a new one.
# Python
aList.append('grape') # Adds 'grape' at the end
print(aList) # Output: ['apple', 'banana', 'cherry', 'grape']
In JavaScript, This line uses the push()
method to add the string grape
to the end of the list. This method is similar to the append() method in Python.
%%js
// Javascript
// Adding 'grape' at the end
aList.push('grape');
// Printing the array
console.log(aList); // Output: ['apple', 'banana', 'cherry', 'grape']
Here’s one more example of these operations in Python
- aList = [1, 2, 3, 4]
- print(aList[1]) # Output: 2
- aList.append(5) # Output: [1, 2, 3, 4,5]
Popcorn Hack
- Try making your own list then apend an element