Python: List Basics


/ Published in: Python
Save to your folder(s)

How to concatenate lists, add an item to a list, slice an item from a list.


Copy this code and paste it in your HTML
  1. #List #1
  2. inventory = ["sword", "armor", "shield", "healing potion"]
  3. #List #2
  4. chest = ["gold", "gems"]
  5.  
  6. #For Loop
  7. for item in inventory:
  8. print item
  9.  
  10. #Concatenating Lists
  11. inventory += chest
  12. print len(inventory)
  13.  
  14. #Adding New Items to the List
  15. inventory[4:4] = "chrisaiv"
  16.  
  17. #Assigning a New List Slice
  18. inventory[0:1] = ["rocks"]
  19. print inventory
  20.  
  21. #Delete a List Slice
  22. del inventory[4:9]
  23. print inventory

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.