jgtrosh

python lists - access by index

Aug 3rd, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. >t = ['a', 'b', 'c', 'd']
  2. >#   0   1    2    3      indexes to the left of values
  3. >t[0]                   # access the value to the right of index
  4. 'a'
  5. >t[0:2]                 # slice the list from index 0 to index 2
  6. ['a', 'b']
  7. >t[1:]                  # slice the list from index 1 to the end
  8. ['b', 'c', 'd']
  9. >t[1:-1]                # slice the list from index 1 to one but last
  10. ['b', 'c']
  11. >t[:]                   # create a copy of the list
  12. ['a', 'b', 'c', 'd']
  13. >#  | a | b | c | d | a | b | c | d |   value
  14. ># -4  -3  -2  -1   0   1   2   3   4   indexes
Advertisement
Add Comment
Please, Sign In to add comment