#python
Common List Methods
There are number of methods for lists, but we will at least cover how to add and delete items from them. All of the list methods can be found on Python's documentation website. Methods follow the list name. In the statement listName.append(2) , append() is the method.
- .append(value) - appends element to end of the list
- .count('x') - counts the number of occurrences of 'x' in the list
- .index('x') - returns the index of 'x' in the list
- .insert('y','x') - inserts 'x' at location 'y'
- .pop() - returns last element then removes it from the list
- .remove('x') - finds and removes first 'x' from list
- .reverse() - reverses the elements in the list
- .sort() - sorts the list alphabetically in ascending order, or numerical in ascending order
If you want to change selection, open document below and click on "Move attachment"
Python Lists Tutorialtold you we would come back to see how awesome the for loop is. Basically, variable a is the actual element in the list. We are incrementing an implicit index. Don't get too worried about it. Just remember we are cycling through the list.
<span>Common List Methods
There are number of methods for lists, but we will at least cover how to add and delete items from them. All of the list methods can be found on Python's documentation website. Methods follow the list name. In the statement listName.append(2), append() is the method.
.append(value) - appends element to end of the list
.count('x') - counts the number of occurrences of 'x' in the list
.index('x') - returns the index of 'x' in the list
.insert('y','x') - inserts 'x' at location 'y'
.pop() - returns last element then removes it from the list
.remove('x') - finds and removes first 'x' from list
.reverse() - reverses the elements in the list
.sort() - sorts the list alphabetically in ascending order, or numerical in ascending order
Try playing around with a few of the methods to get a feel for lists. They are fairly straightforward, but they are very crucial to understanding how to harness the power of Python. Summary
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
Details