How can I subset a dictionary using Python 3? Using Python 2, the below works:
global pair_dict
pair_dict = {
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight'
}
global test_printer
def test_printer(start_chunk, end_chunk):
# .items() replacing .iteritems() from Python 2
sub_dict = dict(pair_dict.items()[start_chunk:end_chunk])
print sub_dict
test_printer(0, 2)
However, in this version of Python I now get the following error:
Traceback (most recent call last):
File "G:/HTPC Scripts/WebGrab Plus/TESTTESTTEST2.py", line 52, in <module>
set_chunk_start_end_points()
File "G:/HTPC Scripts/WebGrab Plus/TESTTESTTEST2.py", line 48, in set_chunk_start_end_points
test_printer(start_chunk, end_chunk)
File "G:/HTPC Scripts/WebGrab Plus/TESTTESTTEST2.py", line 27, in test_printer
sub_dict = dict(pair_dict.values()[start_chunk:end_chunk])
TypeError: 'dict_values' object is not subscriptable