
|
Lesson 11 Notes: Making a List, Checking it Twice
- What is a list?
- A list is an ordered set of elements.
- Lists are enclosed in [ ]
- Each item in a list is separated by a comma
- Examples:
pets = ["dog", "cat", "bird", "elephant"]
nums = [1,2,3,4,5,6]
stuff = [1, 15, "gorilla", 23.9, "alphabet"]
- Lists inside of Lists
- Lists may also contain other lists
- A list inside a list is called a "nested list"
- Example:
to_do = ["clean up", ["feed dog", "feed cat", "feed fish"], "fix dinner"]
- Slicing Lists
- Lists can be accessed just like strings by using the [ ] operators.
- Example:
list = ["Peter", "Andrew", "James", "John"]
print list [0]
>>> Peter
print list [1:]
>>> Andrew, James, John
- Accessing Nested Lists
- To access an item in a nested list, provide two index addresses.
word_list = ["dog", ["fluffy", "spot", "toto"], "mutt"]
print word_list [1] [1]
>>> "spot"
- Mutability
- Unlike strings, lists are mutable, which means they can be changed.
- Example:
string = "dog"
string[0] = "f" # This is not a legal function
- Example:
list = ["d", "o", "g"]
list[0] = "f"
>>> ["f", "o", "g"]
- Dicing Lists
- Lists can also be changed using the [ ] operators.
- Example:
list = ["Mark", "Luke", "John"]
list[0] = "Mattew"
print list
>>> "Matthew", "Luke", "John"
- Adding to Lists
- To add items at the end of a list, use list.append("item")
- Example:
list = ["Matthew", "Mark", "Luke"]
list.append("John")
print list
>>>Matthew, Mark, Luke, John
- Deleting Items from Lists
- Use del to remove item based on index position.
- Example:
list = ["Matthew", "Mark", "Luke", "John"]
del list[1]
print list
>>> Matthew, Luke, John
- Removing Item from List
- To remove an item from a list without using the index position, use list_name.remove("item")
- Example:
kids = ["Bubba", "Sissy", "Froggy"]
kids.remove("Froggy")
print kids
>>> Bubba, Sissy
- What's in a list?
- The keyword "in" can be used to test if an item is in a list.
- Example:
list = ["red", "orange", "green", "blue"]
if "red" in list:
do_something()
- Keyword "not" can be combined with "in".
- Example:
list = ["red", "orange", "green", "blue"]
if "purple" not in list:
do_something()
- for and Lists
- for loops can be used with lists just as they are with strings.
- Example:
email = ["you@email.com", "bob@email.com"]
for item in email:
send_email()
- Other List Tricks
len(list_name)
Gets the length of a list
list_name.sort()
Built-in function to put lists in alphabetical order
random.choice(list_name)
Picks a random item from a list.
string.split(list_name)
Converts a string, like a sentence, into a list of words.
string.join(list_name)
Converts a list of words it into a sentence in string form.
- Automatic Number list with range()
- range():
- Takes start number, end number, and step parameters
- Returns a list of numbers
- Examples:
range(2,10) = [2,3,4,5,6,7,8,9]
range(0,11,2) = [0,2,4,6,8,10]
- Review
- What is a list?
- How does a list differ from a string?
- What is a nested list?
- What is mutability?
- What special functions can be used with lists?
Restricted access |