Ben Poon
Ben Poon

Reputation: 125

Python Dictionary Comprehension accessing non-existent key

I'm trying to create a dictionary that would look like this if n == 3

{
  0: [1, 2],
  1: [0, 2],
  2: [0, 1]
}

Basically.. just a dict with keys that mapped to arrays that contained the other key integers except the one it was mapped to. My code:

import itertools

graph = {i: range(num_nodes) for i in range(num_nodes)}

for i in graph.keys():
    graph[i].remove(i)

I'm not sure why this error is being thrown ValueError: list.remove(x): x not in list

After a bit more research, found that I could do a subtraction between sets and do it all in one go:

graph = {i: list(set(range(num_nodes)) - set([i])) for i in range(num_nodes)}

Thanks everybody!

Upvotes: 1

Views: 354

Answers (3)

sebastian_oe
sebastian_oe

Reputation: 7320

If you want to use a dict comprehension without an additional for loop, try this:

import itertools
num_nodes = 3

graph = {i: (range(num_nodes)[:i] + range(num_nodes)[i+1:]) for i in range(num_nodes)}

Note: Unfortunately, the list concatenation is necessary, as list.remove(x) does not return an updated list but changes the list in place and returns nothing. Thus, range(num_nodes).remove(i-1) cannot be directly used in the comprehension.

Upvotes: 1

ashwinjv
ashwinjv

Reputation: 2967

That is because type(graph[i]) is not a list, its of type range. Convert it to a list and try this:

[Edit: removed subtraction, check sgarza62's comment]

import itertools    

graph = {i: list(range(num_nodes)) for i in range(num_nodes)}

for i in graph.keys():
    graph[i].remove(i)

Upvotes: 2

sgarza62
sgarza62

Reputation: 6238

Prior to the edit, it was an off-by-one error.

import itertools

num_nodes = 3
graph = {i: range(num_nodes) for i in range(num_nodes)}

for i in graph.keys():
    graph[i].remove(i)

print graph
# {0: [1, 2], 1: [0, 2], 2: [0, 1]}

Upvotes: 1

Related Questions