-6

This is what I am trying to go for but i keep getting error any ideas?

third_value_list =[0,1] 
for i in third_value_list 
num_list = [1,2] 
val_list = [0,1] 
dict1 = dict((k, [v]+i) for (k, v) in zip(num_list,val_list)) 
print dict1 

TypeError:can only concatenate list (not 'int') to list

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

2
  • What is the purpose of the third_value_list that you're looping on? If it had multiple values, what would you expect your code to do?
    – Blckknght
    Commented Mar 15, 2016 at 21:34
  • What do you think [v]+i is going to do? Break that second-to-last line into several statements to get a clearer understanding of what part is failing. Commented Mar 15, 2016 at 21:34

4 Answers 4

0

If you want to add a a value to key in your dictionary just make sure the values are, from the start, tuples or lists. Once you do this you can just:

    d = {1:(0,),2:(0,)}
    d[1] = d[1]+(0,)

And a new value is now present at d[1]. Other than that and you might have to build a function to handle the difference between types (list, tuple, int, etc).

0

I am not sure what exactly you are trying to construct, but you can get the desired result simply by

num_list = [1,2]
val_list = [0,1]
dict1 = dict((k, [v, 0]) for (k, v) in zip(num_list,val_list))

The main problem in your code is that you are trying to add a list and an integer

[v]+i

which I replaced with [v,i]. But since i is always 0 you get the answer I posted above.

0

First of all you will never be able to get an output like that because you can not have duplicate keys in a dictionary. Assigning a value to an existing key will wipe out the old value.

0

I add [] and i got the desired output yes it is possible

third_value_list =[0,1] 
for i in third_value_list 
num_list = [1,2] 
val_list = [0,1] 
dict1 = dict((k, [v]+[i]) for (k, v) in zip(num_list,val_list)) 
print dict1 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.