I have a list of data (of varying type) that I need to process. It has fixed length and I know which index holds what data, so currently I do something like
for x in data[:13]:
do_stuff(x)
for x in data[13:21]:
do_other_stuff(x)
...
I know I can split the data manually like
list1 = data[:13]
list2 = data[13:21]
...
but I was wondering if there was a way to do it in one line similar to the normal list unpacking, maybe like
*list1[13], *list2[8], *rest = data
Yes you can do this by slicing in one-liner:
list1, list2, ••• = data[:13], data[13:21], ••• #[1]
The list
is sliced into multiple list
s and stored into the various variables.
[1] The index pattern follows the OP’s index pattern closely.