Why do you get an error?
In Python, that error typically means "You can't slice this object." Strings, lists, tuples, etc are sliceable, but integers are not. The error is raised during iteration as it comes across an integer.
Options
Depending on what results you want, here are some options to try:
- Pre-process your input, e.g. a list of pairs
[('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6)]
.
- Remove the incompatible indices.
- Use a tool to build a dictionary from pairs, e.g.
pip install more_itertools
.
Solution
I suspect you want results similar to option 3:
import more_itertools as mit
lst = ['one', 1, 'two', 2, 'three', 3, 'four', 4, 'five', 5, 'six', 6]
{k: v for k, v in mit.sliced(lst, 2)}
# {'five': 5, 'four': 4, 'one': 1, 'six': 6, 'three': 3, 'two': 2}