Lists Flashcards
Lists
Ordered sequences that can hold a variety of object types
.append
Add a variable to the end of a list
my_list.append(‘seven’)
.pop
Pop a variable off a list. Defaults to the last variable, but can be changed with index number
my_list.pop() / my_list.pop(3)
.sort
Sorts a list in alphabetical or numerical order.
Note: Doesn’t return anything, sorts it in place. Must call the list to see the sorted variables.
my_sorted_list = my_list.sort()
This wont work because of the note above
new_list.sort()
my_sorted_list = new_list
This will work
.reverse
Puts the variables in a list in reverse order. Just like the .sort function it happens in place and does not return anything unless called.
Dictionary vs. List
{ } Dictionaries:
- Unordered and cannot be sorted
- Objects retrieved by key name
[ ] Lists:
- Ordered sequence can be indexed or sliced
- Objects retrieved by key name