-1

I have written a script that executes for a single entry. However I have a database of 17,000 entries, in the form of a dictionary. I would like to perform these calculations on each entry just once, and each calculation output be written to a text file.

The dictionary is called DVG_dict. The key is called "n_seq" The line I used to start the loop is:

for i in range(len(DVG_dict)):

For some reason this code is not working. Any ideas?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Welcome to StackOverflow - the best way to get answers is to provide an example that works all by itself and shows what the problem is. You provided a single line of code, but without knowing what code sits around it, it's hard to say what you're doing wrong. Please provide an example of code that's not working, but that you expected to work, by itself. – Grismar Nov 20 '19 at 23:10
  • Please see https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops?rq=1 – OneCricketeer Nov 20 '19 at 23:12
  • Welcome to StackOverflow. See [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). We cannot effectively help you until you post your MRE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. "not working" is not a problem specification. – Prune Nov 20 '19 at 23:15
  • I suggest that you consult a tutorial on dictionaries; look for the part on iterating through the dict. – Prune Nov 20 '19 at 23:15

2 Answers2

0

Your keys in a dictionary are always unique. I am not sure if I understand, what you mean with "the key is called "n_seq""? try

keys = mydict.keys() // returns list of keys

for item in keys:
  // do something with dict.get(item)
Snake_py
  • 393
  • 2
  • 11
0

The loop you just used is iterating through the number of keys of the dict, not the values of the dict.

I'm guessing you have something like this:

DVG_dict['n_seq'] = list_if_17000_values

So, what you actually want to do is iterate through those 17000 values in the dictionary, right?

Try something like this:

for value in DVG_dict['n_seq']:

there, any value will be one of those in your database, not a index.

If you also want a index, you should do this:

for i, value in zip(DVG_dict['n_sq']):

where i is the index, and value is one of the elements of your database.