"Select a section of list types (arrays, tuples, NumPy arrays) using [start:stop]. start is included, stop is not. The number of elements in the result is stop - start."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq = 'Monty Python'\n",
"seq[6:10]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
"'Pyth'"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Omit start to default to start of the sequence"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq[:5]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"'Monty'"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Omit end to default to end of the sequence"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq[6:]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"'Python'"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Negative indices slice relative to the end"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq[-12:-7]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"'Monty'"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Slice can also take a step such as the one below, which takes every other element"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq[::2]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"'MnyPto'"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Passing -1 for the step reverses the list or tuple:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq[::-1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"text": [
"'nohtyP ytnoM'"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assign elements to a slice. Slice range does not have to equal number of elements to assign."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq = [1, 1, 2, 3, 5, 8, 13]\n",
"seq[5:] = ['H', 'a', 'l', 'l']\n",
"seq"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 7,
"text": [
"[1, 1, 2, 3, 5, 'H', 'a', 'l', 'l']"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Compare assigning into a slice (above) versus assigning into an index (below)"
"Generate a list of evenly spaced integers. Note: range is no longer avalable in Python 3 "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"range(10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"text": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Range can take start, stop, and step arguments"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"range(0, 20, 3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"[0, 3, 6, 9, 12, 15, 18]"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is very common to iterate through sequences by index with range"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq = [1, 2, 3]\n",
"for i in range(len(seq)):\n",
" val = seq[i]\n",
" print(val)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"xrange is recommended for longer ranges and is available in Python 3 as range. It returns an iterator that generates integers one by one rather than all at once and storing them in a large list."
"The bisect module does not check whether the list is sorted, as this check would be expensive O(n). Using bisect on an unsorted list will not result in an error but could lead to incorrect results."