<jupyter_start><jupyter_text>SlicingA common feature of list, tuple, str, and all sequence types inPython is the support of slicing operations, which are more powerful thanmost people realize. Example 2-12. Line items from a flat-file invoice<jupyter_code>invoice = """
0.....6.................................40........52...55........
 1909      Pimoroni PiBrella                $17.50  3
$52.50
 1489     6mm Tactile Switch x20            $4.95   2
$9.90
1510      Panavise Jr. - PV-201             $28.00  1
$28.00
1601      PiTFT Mini Kit 320x240            $34.95  1
$34.95
"""

SKU= slice(0, 6)
DESCRIPTION = slice(6, 40)
UNIT_PRICE = slice(40, 52)
QUANTITY = slice(52, 55)
ITEM_TOTAL = slice(55, None)
line_items = invoice.split('\n')[2:]
for item in line_items:
    print(item[UNIT_PRICE], item[DESCRIPTION])<jupyter_output>$17.50        Pimoroni PiBrella            
 
    $4.95        6mm Tactile Switch x20        
 
    $28.00       Panavise Jr. - PV-201         
 
    $34.95       PiTFT Mini Kit 320x240<jupyter_text>Assigning to Slices<jupyter_code>l = list(range(10))
l
l[2:5] = [20, 30]
l
del l[5:7]
l
l = list(range(7))
print(l)
l[3::2] = [-1, -6]
l
l[2:5]=[9]<jupyter_output><empty_output><jupyter_text>Using + and * with Sequences Example 2-13. A list with three lists of length 3 can represent a tic-tac-toeboard<jupyter_code>board = [['_'] * 3 for i in range(3)]
board
board[1][2] = 'X'
board<jupyter_output><empty_output><jupyter_text>Example 2-14. A list with three references to the same list is useless<jupyter_code>wierd_board = [['_'] * 3] * 3
wierd_board
wierd_board[1][2] = 'O'
wierd_board<jupyter_output><empty_output><jupyter_text>A += Assignment PuzzlerExample 2-15. A riddle<jupyter_code>t = (1, 2, [30, 40])
t[2] += [50, 60]
t
t<jupyter_output><empty_output><jupyter_text>list.sort versus the sorted Built-In<jupyter_code>fruits = ['grape', 'raspberry', 'apple', 'banana']

sorted(fruits, key=len, reverse=True)
fruits.sort()
fruits
import bisect

bisect.bisect([1,2,3,4,5,6], 8)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup