Advertisement
tinypirate

stuff relating to lists

Jun 2nd, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. #lists
  2.  
  3. mlist = [1,2,3,4,5,6,7,8] #prints out specified list index, starts at [0]
  4. print (mlist[1])
  5. >> 2
  6.  
  7. #for in loops saved my life
  8. mlist = [1,2,3,4,5,6,7,8]
  9. for x in mlist:
  10.  print (x) #prints list without the ugly square brackets and commas
  11.  
  12. #useful list-y things
  13. .append(value) #appends element to end of the list
  14. .count('x') #counts the number of occurrences of 'x' in the list
  15. .index('x') #returns the index of 'x' in the list
  16. .insert('y','x') #inserts 'x' at location 'y'
  17. .pop() #returns last element then removes it from the list
  18. .remove('x') #finds and removes first 'x' from list
  19. .reverse() #reverses the elements in the list
  20. .sort() #sorts the list alphabetically in ascending order, or numerical in ascending order
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement