3

I have a dictionary with keys in it but no values:

> {'GROUP': '?', 'THREAD': '?', 'SEQUENCE': '?', 'BYTES': '?', 'BLOCKSIZE': '?'}

I do also have a loop which returns lists of values:

for row in rng:
    result = [d[row] for d in inp]
    print(result)
> ['1', '2', '3']
> ['1', '1', '1']
> ['346', '347', '348']
> ['52428800', '52428800', '52428800']
> ['512', '512', '512']

How could I assign the db.keys to db.values, so the output looks like this:

{'GROUP': ['1', '2', '3'], THREAD': ['1', '1', '1'], 'SEQUENCE': ['1', '1', '1'], 'BYTES': ['52428800', '52428800', '52428800'], etc.}

Should I do values assignment in the loop directly?

2
  • 2
    Dictionaries are unordered, so you would need to impose some particular order on your dictionary before you could assign these values. Commented Mar 19, 2017 at 12:16
  • Question is underspecified. Do you have a fixed order the results should be assigned, or do you want to assign to the key row in each iteration of the loop? Commented Mar 19, 2017 at 12:21

3 Answers 3

2

Since dictionaries are unordered (the keys you've defined are in random order, regardless of which order you define them in), there's no way to know which result list you want to assign to which key.

What I would do instead is create a list or a tuple of the keys to keep the order:

keys = ('GROUP', 'THREAD', 'SEQUENCE', 'BYTES', 'BLOCKSIZE')

Then fetch all the values into another list (or better yet, a generator):

values = ([d[row] for d in inp] for row in rng)

So now you have two iterables in the same order, one with keys and one with corresponding values. Here's what they'd look like as lists (values is a generator so we can't really print the values):

>>> print(keys)
['GROUP', 'THREAD', 'SEQUENCE', 'BYTES', 'BLOCKSIZE']
>>> print(values)
[['1', '2', '3'], ['1', '1', '1'], ['346', '347', '348'], ['52428800', '52428800', '52428800'], ['512', '512', '512']]

Now it's easy to construct the final result dictionary with the built-in zip() function:

data = dict(zip(keys, values))
Sign up to request clarification or add additional context in comments.

Comments

0

If the order of keys you want assign to is fixed, then build an iterator that spits out the keynames in whatever order you want.

keynames = iter(['GROUP', 'THREAD', 'SEQUENCE', 'BYTES', ...])

for row in rng:
    # your code
    ...

    # new code
    key = next(keynames)
    d[key] = result

If you want to assign to the key row in each iteration, just do it:

for row in rng:
    # your code
    ...

    # new code
    d[row] = result

(Assuming d is your dictionary you want to assign to, change as needed.)

Comments

-1

To follow with the input structure you've shared in question, you can use dict comprehension. That is,

{k:result[index] for index, k in enumerate(yourdict.keys())}

yourdict is the dictionary that you've given in your question.

Another solution is by following Markus answer.

dict(zip(yourdict.keys(),result)

Hope this helps!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.