Reputation: 12084
I have a list of dictionaries in Python as follows:
images = [{'uri': 'https://some-link.blob.core.windows.net.net/devthumbnails/1024x768/64/41/98/64419888297779407561461629073227065160696285270116603463558823470505654333611.jpg',
'position': '0'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/33/44/41/33444169622672594337676840600346050570907940224888845885580705086781040628903.jpg',
'position': '1'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/41/46/99/41469935475840270004889223861351938003980938652350786820222101550878440209095.jpg',
'position': '2'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/58/75/58/5875586317181716679126235880198721021684963481995594779485630886902274081554.jpg',
'position': '3'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/43/49/66/43496617617402984595952294447831932918405619473544645594784712841815014159696.jpg',
'position': '4'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/58/02/34/58023409863921180862667204492888551278254010639571762445601939880620855964505.jpg',
'position': '5'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/36/22/76/36227680451273880975070000838700274536225673420153023620631555589946521174102.jpg',
'position': '6'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/25/08/19/25081934413799694457632686505539828478363116795387601261763375409872686844488.jpg',
'position': '7'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/29/69/32/29693252042243727029651750357989431990778953906071901135228406135133977694240.jpg',
'position': '8'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/32/08/12/32081296724310348807184322021859931042996444008194149989050172920699516702708.jpg',
'position': '9'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/78/32/05/78320563911470703705431807857066302926786080992451285767704219765878587895294.jpg',
'position': '10'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/45/68/68/45686886529184531303217514851187276286410962612950239302976824977433885355299.jpg',
'position': '11'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/72/59/00/7259007439354083322229206921563369468125207106159648770203873465284114242890.jpg',
'position': '12'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/84/48/54/84485459564719601932735074276164902175816462482914733108723651993560201088913.jpg',
'position': '13'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/10/28/77/102877279417898628351098113431759126412045721643126079051218817213834246455153.jpg',
'position': '14'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/39/83/38/39833822893039666936949954403817914899073341336875871633644019565696649530535.jpg',
'position': '15'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/99/45/89/99458973667381587321791276054667596349042589326181154771316565893893148706844.jpg',
'position': '16'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/82/61/29/82612936862475837022828306892947119148350841132548959316321620668433031577983.jpg',
'position': '17'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/11/19/56/111956100385260490476900173411198572352351708852063055906233069090064975146790.jpg',
'position': '18'}]
I want to sort it and I want that the new list is as follows: [first_three_elements, last_three_elements, the_rest]
.
I've tried with something as follows:
first_three = images[0:3]
the_rest = images[3:int(len(images) - 3)]
last_three = images[(int(len(images) - 3)):int(len(images))]
result = first_three + last_three + the_rest
But I get an error:
unhashable type: 'slice'
unhashable type: 'slice'
unhashable type: 'slice'
Any idea how to solve it?
Upvotes: 0
Views: 69
Reputation: 82805
Looks like you have a dictionary.
Ex:
images = {"images" : [{'uri': 'https://some-link.blob.core.windows.net.net/devthumbnails/1024x768/64/41/98/64419888297779407561461629073227065160696285270116603463558823470505654333611.jpg',
'position': '0'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/33/44/41/33444169622672594337676840600346050570907940224888845885580705086781040628903.jpg',
'position': '1'},
{'uri': 'https://some-link.blob.core.windows.net/devthumbnails/1024x768/41/46/99/41469935475840270004889223861351938003980938652350786820222101550878440209095.jpg',
'position': '2'},
.........
Try:
first_three = images['images'][:3]
the_rest = images['images'][3:-3]
last_three = images['images'][-3]
result = first_three + last_three + the_rest
print(result)
Using below snippet here would raise the mentioned error.
first_three = images[0:3]
Traceback (most recent call last):
File "....", line 75, in <module>
first_three = images[0:3]
TypeError: unhashable type: 'slice'
Upvotes: 0
Reputation: 1432
images = images[:3] + images[-3:] + images[3:-3]
Explanation: Adds the first three elements [:3]
to the last three elements [-3:]
to the rest of the elements [3:-3]
.
(This assumes that your list has at least 6 elements.)
See Slicing, Dicing and Splicing
Upvotes: 2