Array Slice

In PythonLanguage:

    >>> ar = range(100, 110)
    >>> ar
    [100, 101, 102, 103, 104, 105, 106, 107, 108, 109]
    >>> ar[4]   # the fifth element
    104
    >>> ar[:5]  # first five elements
    [100, 101, 102, 103, 104]
    >>> ar[5:]  # from element #5 onwards
    [105, 106, 107, 108, 109]
    >>> ar[-3:]  # from the third-last onwards
    [107, 108, 109]
    >>> ar[4:6]  # from element #4 (incl) to element #6 (excl)
    [104, 105]
    >>> ar[:-4]  # from beginning to the #4 last element (excl).
    [100, 101, 102, 103, 104, 105]
    >>> ar[1:8:2] # every second element from #1 (incl) to #8 (excl)
    [101, 103, 105, 107]


CategoryJargon


EditText of this page (last edited September 19, 2004) or FindPage with title or text search