-1

I have a list of words like

["abc","hello","i"]

and I want to store these in a dictionary where the key is the length of the word.

I have only gotten as far as initializing the dictionary.

ASm
  • 379
  • 4
  • 10
  • 20
  • Do you actually need to sort the list before putting it into the dictionary? – Scooter Oct 07 '14 at 09:45
  • Do you want a dictionary with integers as keys and list of words as values? –  Oct 07 '14 at 09:46
  • @Scooter I don't want to sort them, in the end I just want to be able to print all the one letter words/two letter words etc. – ASm Oct 07 '14 at 09:47
  • @Tichordroma Yes...if I have interpreted that right. I want key: value is word length : word for example {1: ["i"], 2,......} – ASm Oct 07 '14 at 09:49
  • wouldn't you run into having duplicate key if your list grows? – Anzel Oct 07 '14 at 09:52

7 Answers7

3

Use itertools.groupby function, and a dict expression to build the result. The pythonic way

>>> from itertools import groupby    
>>> data=["abc","a", "bca", "hello","i"] #more than one result by word-length
>>> keyfunc = lambda x:len(x)
>>> data = sorted(data, key=keyfunc)
>>> {k:list(g) for k, g in groupby(data,keyfunc)}
{1: ['a', 'i'], 3: ['abc', 'bca'], 5: ['hello']}    
xecgr
  • 5,095
  • 3
  • 19
  • 28
1

In the old days before defaultdict, we use to write where l is the list of words:

d = {}
for w in l:
  if len(w) in d:
      d[len(w)].append(w)
  else:
      d[len(w)] = [w]

Which results in d being {1: ['i'], 3: ['abc'], 5: ['hello']}.

With defaultdict:

from collections import defaultdict
d = defaultdict(list)
for w in l:
    d[len(w)].append(w)

Which results in d being defaultdict(<type 'list'>, {1: ['i'], 3: ['abc'], 5: ['hello']}). And can be easily turned into a dict via dict(d), resulting in {1: ['i'], 3: ['abc'], 5: ['hello']}.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • Ah this is exactly what I wanted to do, where I was going wrong was the append. – ASm Oct 07 '14 at 09:54
0

You just have to

array = ["abc","hello","i"]
dict = {}
for i in array:
    dict[len(i)] = i

The main problem is that in a dictionary can be put only one value per key, so if you have more than one word that have the same length, only the last world you put in will be saved.

aadeg
  • 13
  • 1
  • 1
    "so if you have more than one word that have the same length, only the last world you put in will be saved" If you know this, why post this "answer"? –  Oct 07 '14 at 09:57
0
words = {}
words[3] = "abc"
words[5] = "hello"
words[1] = "i"
HCP
  • 1,012
  • 2
  • 11
  • 18
  • If I look at the comments this seems to be what you want. Please others comment if I am wrong – HCP Oct 07 '14 at 10:00
  • I think (hope) he wants a solution that works for every list of string. You could have writter directly `{1: "i", 3: "abc", 5: "hello"` as an answer. – Dettorer Oct 07 '14 at 10:01
0

Similarly, it can be done with the following code:

d = {}
l = ["abc","hello","i"]
for item in l:
    d.setdefault(len(item), []).append(item)
Youmu
  • 78
  • 4
0

you can try following code:

dict = {}
elements = ["varun","rahul","rishabh"]
for item in elements:
    dict.setdefault(len(item), []).append(item)
Varun Chadha
  • 376
  • 2
  • 17
0

Using a comprehension:

mydict = {len(i): i for i in list_of_words}

Edit: for updated OP needs this is not useful - it only records one word per word length key. I can't figure out how to do the equivalent of perl's

push @list, value

with autovivification.