Advertisement
here2share

# list_manip.py

Apr 10th, 2015
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. # list_manip.py
  2.  
  3. L = ['yellow', 'red', 'blue', 'green', 'black']
  4.  
  5. print L, 'is the example list'
  6. print 'List is of', len(L), 'colors'
  7. print sorted(L), 'is in alphabetical order'
  8. append_color = 'purple'
  9. L.append(append_color)
  10. print L
  11. print 'Added', append_color, 'to the end of the list'
  12. add_color = ['gold']*5
  13. L += add_color
  14. print L
  15. print 'Added a list of', add_color, 'to the list'
  16. number_of_gold = L.count('gold')
  17. print 'There are', number_of_gold, 'instances of gold in the list'
  18. L.remove('gold')
  19. print 'The first of instance of gold has been removed'
  20. L = [new_L for new_L in L if new_L != ('gold')]
  21. print L
  22. print 'Each of the instances of gold has been removed'
  23. insert_color = 'white'
  24. L.insert(3, insert_color)
  25. print L
  26. print 'Inserted', insert_color, 'as forth* item in the list (0,1,2,*3)'
  27. popped_color = L.pop()
  28. print L
  29. print 'pop() removed last item, which is', popped_color, ', from the list'
  30. popped_color = L.pop(2)
  31. print L
  32. print 'pop(2) removed', popped_color, 'from the list'
  33. L.reverse()
  34. print L
  35. print 'List has been reversed'
  36. if 'red' in L:
  37.     print 'list contains red'
  38. print
  39. for item in L:
  40.     print item
  41. print
  42. import random
  43. R = list(range(25))
  44. L += R
  45. random.shuffle(L)
  46. print L
  47. L = list(set(L) - set(R))
  48. print
  49. print L
  50. print 'The items of numbers has been removed from the list of colors'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement