List Methods
There are lots of things you can do to a list. The following summary uses the list called x:
- add
- x.append(y)
- y is an integer or string in quotes
- count
- x.count(y)
- y is how many times the number in the brackets is in your list
- index
- x.index(y)
- y is the item you're looking for in the list and it tells you what place in the list it is located
- insert
- x.insert(a,b)
- a = the spot in list x that you want to insert into (keep in mind it starts at 0)
- b = what you're inserting into that spot
- remove
- x.remove(y)
- this removes the value y from the list x
- reverse
- x.reverse()
- this reverses the order of list x
- extend
- x.extend([a,b,c,d])
- this adds a,b,c,d to the list x
- pop
- x.pop()
- this removes the last item of the list
- x.pop(y)
- this removes the item at index spot y from the list (remember that it starts at 0)
- sort (we may do this one later, but it's not included in this lesson)
Type in the following program. The comments are optional but are there to help you tell what they do!