3

I have a dictionary that has keys of different word lengths, for example:

d={'longggg':'a', 'short':'b', 'medium':'c', 'shor':'d'}

and I want to end up with a dictionary that only has keys that are greater than a certain length. For example, I want to only keep entries that are 6 letters long or more. So I want

new_d={'longggg':'a', 'medium':'c'}.

I tried

new_d=dict(k,v) for k,v in d.items() if len[k]>=6

and

new_d={}
for k, v in d.items():
    if len[k]>=6:
        new_d.update({k:v})

along with many other variations of that code, but the problem ends up being in taking the length of a key.

Dana
  • 55
  • 3
  • 2
    `new_d={k:v for k,v in d.items() if len(k)>=6}`. The square brackets you used in `len[k]` are for indexing, you instead wanted to call the function, so use parentheses. – roganjosh Oct 19 '17 at 18:51
  • 1
    Which version of Python? d.items() only works in version 3+ – Daniel Gale Oct 19 '17 at 18:58
  • 1
    @DanielGale not true. The comment I made was tested in Python 2 before I posted it. – roganjosh Oct 19 '17 at 18:59
  • @roganjosh I do not see .items() in the documentation for 2.7 only iteritems(). In the version 3 docs I see items() not iteritems() https://docs.python.org/2.7/tutorial/datastructures.html – Daniel Gale Oct 19 '17 at 19:03
  • 1
    @DanielGale https://stackoverflow.com/questions/10458437/what-is-the-difference-between-dict-items-and-dict-iteritems `iteritems` was added to create a generator, a bit like `xrange` for `range`, which became the default `range` in Python 3. – roganjosh Oct 19 '17 at 19:05
  • @roganjosh Thanks, so for what it seems in 2.7 .items() exist but may take a lot of memory so .iteritems() were developed. In version 3, .items() was fixed to act like iteritems() and iteritems() was removed. In 2.7, you should use iteritems() and in version 3 you should use items() – Daniel Gale Oct 19 '17 at 19:10
  • 1
    @DanielGale exactly :) – roganjosh Oct 19 '17 at 19:11

3 Answers3

2

Use Dictionary comprehensions. No need to do for k in d.keys(). Just use for k in d as d.keys() will return a list which is not needed at all. (A lesson I learnt from Stackoverflow itself!!)

Also as @roganjosh pointed out use len() instead of len[] (len() is a function). Square brackets are used for indexing in say, lists and strings.

d={'longggg':'a', 'short':'b', 'medium':'c', 'shor':'d'}

a = {k:d[k] for k in d if len(k)>=6}
print a

Output:

{'medium': 'c', 'longggg': 'a'}
Miraj50
  • 4,257
  • 1
  • 21
  • 34
1

You can try this:

d={'longggg':'a', 'short':'b', 'medium':'c', 'shor':'d'}
final_d = {a:b for a, b in d.items() if len(a) >= 6}

Output:

{'medium': 'c', 'longggg': 'a'}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0

len is a built-in function in Python, and therefore uses parentheses (not the square brackets operator).

The big issue (among other things) with your first solution is that you are creating a separate dictionary for each k, v.

Your second solution should work if you fix the len function call, but I would rewrite new_d.update({k:v}) as new_d[k] = v, since that's the standard way to use a Python dictionary.

I can tell you're new, and the best resource for beginner's questions like these will be the Python documentation rather than Stack Overflow. You should also try copy and pasting your error output into Google. You'll probably be able to solve your problems quicker and you'll get more meaningful answers.