Python: Lists: Take a Slice, Delete Elements, Popping Elements

  1. Introduction to Python
  2. Python: Choosing a Text Editor or IDE
  3. Python: Hello World
  4. Python: Variables, Strings and Numbers
  5. Python: Variable Naming
  6. Python: Math, Familiar
  7. Python: Math, Less Familiar
  8. Python: Mathematical Order of Operations
  9. Python: Introducing PEP 8
  10. Python: Text Concatenation
  11. Python: if Statements and Comparison Operators
  12. Python: else and elif statements
  13. Python: Testing Multiple Conditions
  14. Python: Testing Sets of Conditions
  15. Python: Nested if Statements
  16. Python: Lists
  17. Python: Adding To and Changing Lists
  18. Python: Lists: Take a Slice, Delete Elements, Popping Elements
  19. Python: Tuples
  20. Python: for Loops
  21. Python: Nested for Loops
  22. Python: Capturing and Formatting User Input
  23. Python: Dictionaries
  24. Python: Functions
  25. Python: While Loops
  26. Python: Data Files
  27. Python: Using Pexpect
  28. Python : Using Pexpect :
  29. Python : Using Pexpect:
  30. Python: DCL Conversion to Python

Go to Chapter 17 of A Smarter Way.

Return to our lists page:
…and scroll down slightly because the page TOC is (again) broken.

Taking a slice from a list:

friends = ['bill', 'jeff', 'jim', 'scott', 'joel']

# Grab the first three users in the list.
first_three = friends[0:3]

for friend in first_three:

Now go to Chapter 18 of A Smarter Way.

Remove an item by position:

del friends[0]

Remove an item by value:


Now go to Chapter 19 of A Smarter Way.

Popping an element:

This is a unique operation. Popping an item removes the top item from a list, and passes it to a variable or other operation:

first_friend = friends.pop(0)


See, and

See … and scroll down slightly.

  1. Continue editing
  2. Slice a range of items from your list.
  3. Delete one item by index.
  4. Delete one item by value.
  5. Pop a value from the list, put it into a
  6. Perform a test to see if an item is in your list.