4

All the questions I've seen do the exact opposite of what I want to do:

Say I have a list:

lst = ['a','b','c']

I am looking to make a dictionary where the key is the element number (starting with 1 instead of 0) and the list element is the value. Like this:

{1:'a', 2:'b', 3:'c'}

But for a long list. I've read a little about enumerate() but everything I've seen has used the list element as the key instead.

I found this:

dict = {tuple(key): idx for idx, key in enumerate(lst)}

But that produces:

{'a':1, 'b':2, 'c':3}

... which is the opposite of what I want. And, also in a weird notation that is confusing to someone new to Python.

Advice is much appreciated! Thanks!

q-compute
  • 641
  • 2
  • 7
  • 15
  • See this answer: https://stackoverflow.com/a/4576128/7062327 – Brett Jeffreson Nov 21 '17 at 04:14
  • 1
    The "weird notation" is called a dictionary comprehension. Comprehensions are super handy, and if you'll see a lot of them. Here's a decent place to start reading: https://www.smallsurething.com/list-dict-and-set-comprehensions-by-example/ – Patrick Haugh Nov 21 '17 at 04:17
  • @PatrickHaugh So is dictionary comprehension necessary to do certain things or is it just more concise? As in, can those sort of things be written as normal for loops? If that question makes sense. – q-compute Nov 21 '17 at 04:31
  • 1
    @hexaneandheels No, you can always unroll the comprehension into a `for` loop. It's for brevity and clarity. Here's a document that touches on the Rationale behind them, and includes some more examples: https://www.python.org/dev/peps/pep-0274/#rationale – Patrick Haugh Nov 21 '17 at 04:36

4 Answers4

13

enumerate has a start keyword argument so you can count from whatever number you want. Then just pass that to dict

dict(enumerate(lst, start=1))

You could also write a dictionary comprehension

{index: x for index, x in enumerate(lst, start=1)}
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

By default enumerate start from 0 , but you can set by this value by second argument which is start , You can add +1 to every iterator if you want to start from 1 instead of zero :

print({index+1:value for index,value in enumerate(lst)})

output:

{1: 'a', 2: 'b', 3: 'c'}

Above dict comprehension is same as :

dict_1={}
for index,value in enumerate(lst):
    dict_1[index+1]=value
print(dict_1)
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
  • Instead of adding 1 you can use the `start` argument of `enumerate`, which indicates what number the enumeration should start at. – Arthur Khazbs Mar 27 '20 at 22:09
0

Using Dict Comprehension and enumerate

print({x:y for x,y in enumerate(lst,1)})

{1: 'a', 2: 'b', 3: 'c'}

Using Dict Comprehension , zip and range-

print({x:y for x,y in zip(range(1,len(lst)+1),lst)})

{1: 'a', 2: 'b', 3: 'c'}

0

I think the below code should help.

my_list = ['A', 'B', 'C', 'D']
my_index = []
my_dict = {}

for i in range(len(my_list)): 
    my_index.append(i+1)

for key in my_index: 
    for value in my_list: 
        my_dict[key] = value
Punith Raj
  • 1
  • 1
  • 1