-3

This is based on this question: python convert list to dictionary

The asker provides the following:

l = ["a", "b", "c", "d", "e"]

I want to convert this list to a dictionary like:

d = {"a": "b", "c": "d", "e": ""}

While the grouper recipe is quite interesting and I have been "playing" around with it, however, what I would like to do is, unlike the output dictionary wanted by that asker, I would like to convert a list with strings like the one above into a dictionary where all values and keys are the same. Example:

d = {"a": "a", "c": "c", "d": "d", "e": "e"}

How would I do this?


Edit:

Implementation code:

def ylimChoice():

    #returns all permutations of letter capitalisation in a certain word.
    def permLet(s):
        return(''.join(t) for t in product(*zip(s.lower(), s.upper())))

    inputList = []
    yesNo = input('Would you like to set custom ylim() arguments? ')
    inputList.append(yesNo)
    yes = list(permLet("yes"))
    no = list(permLet("no"))

    if any(yesNo in str({y: y for y in yes}.values()) for yesNo in inputList[0]):
        yLimits()
    elif any(yesNo in str({n: n for n in no}.values()) for yesNo in inputList[0]):
        labelLocation = number.arange(len(count))
        plot.bar(labelLocation, list(count.values()), align='center', width=0.5)
        plot.xticks(labelLocation, list(count.keys()))
        plot.xlabel('Characters')
        plot.ylabel('Frequency')
        plot.autoscale(enable=True, axis='both', tight=False)
        plot.show()
Community
  • 1
  • 1
  • 1
    What would be the point of such a construct? – TigerhawkT3 Sep 24 '16 at 18:32
  • I want a dictionary of all possible permutations of a word to then run in my script. I found it was just simplest to put all the possible permutations of the capitalisation of a letter into a list, however, I would need to then put it in a dict to make simpler what I need to do. – Walid Mujahid وليد مجاهد Sep 24 '16 at 18:38
  • If you want all permutations take a look at this http://stackoverflow.com/questions/8306654/finding-all-possible-permutations-of-a-given-string-in-python – Pablo Sep 24 '16 at 18:40
  • @Pablo It was easy enough to figure out how to get the permutation of letter capitalisation, however, I just needed to figure out how to get the output, a list, into a dictionary. – Walid Mujahid وليد مجاهد Sep 24 '16 at 18:44
  • Create an empty `dict` like `h = {}` and then you can do `[h[x] = x for x in my_permutation_list]`. I still don't understand what you need the dictionary for. --edit: or you can use a dict comprehension like Martijn Pieters did. – Pablo Sep 24 '16 at 18:50
  • Sure you can, just post it here. The reason why we can't give you a "better" answer is that is unclear as what your goal is. Or at least I don't see it. – Pablo Sep 24 '16 at 18:59
  • @Pablo - You can't assign like that in a comprehension. – TigerhawkT3 Sep 24 '16 at 19:37
  • @Pablo I have edited my question to add the part that I was asking the question for. At least, the implementation of the answers I have received. If I am missing something fundamental, please do tell me :-) – Walid Mujahid وليد مجاهد Sep 24 '16 at 20:35

2 Answers2

5

You can just zip the same list together:

dict(zip(l, l))

or use a dict comprehension:

{i: i for i in l}

The latter is faster:

>>> from timeit import timeit
>>> timeit('dict(zip(l, l))', 'l = ["a", "b", "c", "d", "e"]')
0.850489666001522
>>> timeit('{i: i for i in l}', 'l = ["a", "b", "c", "d", "e"]')
0.38318819299456663

This holds even for large sequences:

>>> timeit('dict(zip(l, l))', 'l = range(1000000)', number=10)
1.28369528199255
>>> timeit('{i: i for i in l}', 'l = range(1000000)', number=10)
0.9533485669962829
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

You can use a dict-comprehension like this:

l = ["a", "b", "c", "d", "e"]
d = {s: s for s in l}
David Zwicker
  • 23,581
  • 6
  • 62
  • 77