9

I am trying to create a dictionary by using a for loop. I have an ordered list, and I am trying to match up the values from the list to ordered numbers. For Example: {0: 100, 1: 423, 2: 434}

I am just having trouble making the for loop.

list = [102, 232, 424]
count = 0
d = {} #Empty dictionary to add values into

for i in list:
    #dictionary key = count
    #key.append(i)
    count+=1

So in the for loop I essentially want to make the count variable the key, and have the corresponding item in the list as the value. Then I would add one to count, and continue. Also I am sorry if my code is a little unclear in the for loop. That isn't actual code, but is just a general idea of what I was looking for. Can anyone help me? Thank you.

XxEthan70xX
  • 153
  • 2
  • 2
  • 5

4 Answers4

12

You set an item in a dictionary by doing dictionary[key] = item, so in your case you would do:

list = [102, 232, 424]
count = 0
d = {} #Empty dictionary to add values into

for i in list:
    d[count] = i
    count+=1
Pedro Castilho
  • 10,174
  • 2
  • 28
  • 39
7

You can use enumerate:

list = [102, 232, 424]
d = {}

for a, b in enumerate(list):
    d[a] = b

or, using dictionary comprehension:

d = {a:b for a, b in enumerate(list)}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
2

You can do this in one line without using a For loop or a Counter variable:

dict(zip(range(0, len(list)), list))
sparkonhdfs
  • 1,313
  • 2
  • 17
  • 31
1

This is a more complicated version of the problem but it is still essentially creating a dictionary with a for loop. I wanted to create a database of all my photos/videos on my computer - 1000s of them and create a dictionary record for each photo which used the name of the photo/movie to identify the record. That way I would have unique dictionary records from which to create the database Step to achieve this.
1) Using another StackOverflow answer but modifying it slightly I got the list of files and their Modified date, (which on Windows better reflected the date they were taken, rather than created date, which was the date the files was created on the Windows machine):

from stat import S_ISREG, ST_MTIME, ST_MODE
import os, sys, time
destdir="C:/Pictures/jiphone/jiPhone-2017"
# get all entries in the directory w/ stats
entries = (os.path.join(destdir, fn) for fn in os.listdir(destdir))
entries = ((os.stat(path), path) for path in entries)

# leave only regular files, insert creation date
entries = ((stat[ST_MTIME], path)
           for stat, path in entries if S_ISREG(stat[ST_MODE]))

2) Then I created the dictionary

dict_J_iphone_2017=dict()

3) Then in a For loop over the entries I made recFile (the file record) from the name of the file, removing the fullstop (but you could have made it from whatever you were looping over)

for cdate, path in sorted(entries):
    fullDate=time.ctime(cdate)
    basePath=os.path.basename(path)
    recFile=basePath.replace(".","")
    etc

4) I then created the dictionary entry for each photo/movie

  dict_J_iphone_2017[recFile]={'name': recName, 'date': recDate, 'time':recTime, 'day': recDay,'tags': [], 'folder': recFolder, 'comment': recComment, 'rating': 0}

and I am able to reference each item in the dictionary by putting in the file's actual name e.g.

print(dict_J_iphone_2017['JiP6_1291PNG']
#{'date': '5-Mar-2017', 'folder': 'C:/Pictures/jiphone/jiPhone-2017', 'tags': [], 'comment': 'PNG', 'name': 'JiP6_1291.PNG', 'time': '16:19:19', 'rating': 0, 'day': 'Sun '}

Hope that helps.