1

Not to be confused with this question in Stackoverflow.

I have a list called a = [2, 3, 4, 1]

I have some function say func(), which is as follows:

def func(a):
    o = []
    n = len(a)
    for i in range(n):
        x=a[:]
        x[i],x[(i+1)%n] = x[(i+1)%n],x[i]
        o.append(x)
    return o

and func(a) produces another list as follows:

[[3, 2, 4, 1], [2, 4, 3, 1], [2, 3, 1, 4], [1, 3, 4, 2]]

Now I want to map the output list to the list from which it is generated. So, how to generate a dictionary in the following format:

a            : o

key          : value1, value2........last value

[2, 3, 4, 1] : [3, 2, 4, 1], [2, 4, 3, 1], [2, 3, 1, 4], [1, 3, 4, 2]
1
  • Can't you just do: {tuple(a): func(a)}? (Minor detail: you can't have a list as a key so I converted it into a tuple instead.) The real question is why you want this, since all you have is a dictionary with one element – do you have multiple values of a? Commented Dec 26, 2014 at 18:13

1 Answer 1

2

Keys in a dictionary cannot be mutable type. You can have a tuple instead. That is

(2, 3, 4, 1) : [3, 2, 4, 1], [2, 4, 3, 1], [2, 3, 1, 4], [1, 3, 4, 2]

This can be done as

def func(a):
    o = []
    n = len(a)
    for i in range(n):
        x=a[:]
        x[i],x[(i+1)%n] = x[(i+1)%n],x[i]
        o.append(x)
    return {tuple(a):o}

For example func([2,3,4,1]) will now return

{(2, 3, 4, 1): [[3, 2, 4, 1], [2, 4, 3, 1], [2, 3, 1, 4], [1, 3, 4, 2]]}

Also note: according to documentation :

The only types of values not acceptable as keys are values containing lists or dictionaries or other mutable types that are compared by value rather than by object identity, the reason being that the efficient implementation of dictionaries requires a key’s hash value to remain constant

POST COMMENT EDIT

You can access the keys directly usin [] notation.

E.g:

l = [2,3,4,1]
a =  func(l)
print (a[tuple(l)])

This will print the list of values.

Or you can loop through the entire dictionary

for i in a.items():
     for j in i:
          print (j)

This will print

 [3, 2, 4, 1]
 [2, 4, 3, 1] 
 [2, 3, 1, 4]
 [1, 3, 4, 2]
Sign up to request clarification or add additional context in comments.

6 Comments

So how to written the key now? Key in our case is tuple(o).? Banagalorean helping bangalorean :D
@Technopolice Now the key is tuple(o). What do you mean by writing to a key ?
I want to retrieve the key from the newly formed dictionary. So how to retrieve it? Like for key, value in such_dictionary: print key ... will throw an error
@Technopolice if you do for key,value in some_dict.items() it should work fine. Note that for key in some_dict works as well.
In this case type of key is tuple, you can convert to list. e.g >>> list(func([2,3,4,1]).keys()[0])
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.