1

Lets call my list of Lists tempList. It has the following structure with 1000 rows, obviously without the brackets:

['foo', u'18,206,600']
['a', u'18,052,573']
['ta', u'17,870,276']
['sc', u'17,792,892']
['wo', u'17,736,288']
['pis', u'17,721,111']

Now I execute the following code and hope that the element on the left is going to be the key and the element on the right the value at each row:

subsDict = dict(tempList)

and get a dictionary, that has a size that is just the fraction of tempList's size. The order of the few dictionary elements is completely shuffled instead of descending as in tempList. I've also tried the suggested solutions here. what am i missing?

EDIT: here is part of the output:

Fitness   :   6,209,230
fffffffuuuuuuuuuuuu   :   782,908
rickandmorty   :   669,563
dataisbeautiful   :   12,447,856
relationships   :   830,326
GlobalOffensive   :   552,751
Showerthoughts   :   13,017,249
blog   :   16,064,085
woahdude   :   1,424,895
artre
  • 311
  • 4
  • 14
  • Your `key`s aren't unique in the dataset - do you want to be a list for each key or...? – Jon Clements Oct 21 '17 at 14:27
  • Please give an example of a portion of the output -- it's not clear what you're getting or what you expected to happen. – trent Oct 21 '17 at 14:28
  • your code seems fine, as I cannot replicate this error on my machine. – Ajax1234 Oct 21 '17 at 14:29
  • 2
    @artre okay... is your actual `tempList` something like: `[['a', u'something'], ['b',u'something else']]` - so it's definitely an iterable of 2-items and the first item is definitely unique? – Jon Clements Oct 21 '17 at 14:32
  • @JonClements yes exactly. and the first value at each row is unique. im quite sure about that – artre Oct 21 '17 at 14:42
  • @artre Like I said, do the little exercise in my answer and see what happens. – cs95 Oct 21 '17 at 14:44

1 Answers1

1

Your keys definitely aren't unique. To confirm this, do something like:

keys = [x[0] for x in tempList]
print(len(keys), len(set(keys))

They should be different, with the latter being smaller. Now, from reading your question, it seems you want two things:

  1. Order
  2. Preservation of all your values

The answer to both these requirements is to use an OrderedDict of lists.


from collections import OrderedDict

subDict = OrderedDict()
for x in tempList:
    subDict.setdefault(x[0], []).append(x[1])
cs95
  • 379,657
  • 97
  • 704
  • 746
  • ok i did the test above. len(keys) gives me 1000 and len(set(keys)) gives me 125. – artre Oct 21 '17 at 14:45
  • @artre It should be clear now that your keys are _not_ unique... you only have _125_ unique keys, out of 1000. – cs95 Oct 21 '17 at 14:46
  • ok. that i understand. though on the output i cant see any duplicates, they all seem to be the unique key IDs. thats weird. ill have to go back to the previous step and see what ive done wrong there while creating the list. – artre Oct 21 '17 at 14:50
  • @artre Or you could just use an OrderedDict, like in my answer? ;-) – cs95 Oct 21 '17 at 14:51
  • i guess i could :-)) . im just trying to find the bug in my "seemingly correct" code. – artre Oct 21 '17 at 14:52