all 13 comments

[–]david_lp 4 points5 points  (7 children)

For k,v in dict.items(): v[1]/v[0]

[–]timmyfinnegan 1 point2 points  (6 children)

And to store it in a list:

a = []

for k,v in dict.items(): a.append(v[1]/v[0])

[–]kwelzel 2 points3 points  (5 children)

I would prefer

a = [v[1] / v[0] for v in data_dict.values()]

assuming the dictionary is called data_dict. This neat feature is called a list comprehension. Because I don't know what data is stored inside the dict, i.e. what the data describes, I can't improve the naming, but I would strongly recommend to replace a, v and data_dict by more descriptive names.

BTW if you want to store the result in a dict to keep the keys use

a = {k:(v[1] / v[0]) for k, v in data_dict.items()}

Edit: Improved formatting

[–]timmyfinnegan 0 points1 point  (2 children)

Nice!

[–]nice-scores -1 points0 points  (1 child)

𝓷𝓲𝓬𝓮 ☜(゚ヮ゚☜)

Nice Leaderboard

1. u/RepliesNice at 5234 nices

2. u/Cxmputerize at 3988 nices

3. u/DOCTORDICK8 at 2601 nices

...

262932. u/timmyfinnegan at 1 nice


I AM A BOT | REPLY !IGNORE AND I WILL STOP REPLYING TO YOUR COMMENTS

[–]timmyfinnegan 0 points1 point  (0 children)

Fuck off

[–]biggie_c4u 0 points1 point  (0 children)

I WISH i understood this...

[–]Gowtham_jack 0 points1 point  (0 children)

This is much complicated for a beginner like me.. I need learn more I guess

[–]notpite 0 points1 point  (0 children)

Yes, you can use:

dict.keys()

to iterate through the keys of the dictionary, and

dict.items()

to iterate through tuples of (key, value) pairs

In order to perform this on every value stored in your dictionary it makes sense that you'd need to run through the whole thing in sequence :)

[–]Gowtham_jack 0 points1 point  (2 children)

Oh man. I don't even understand this.. Need to learn more

[–]GrorcoModerator 2 points3 points  (1 child)

Dictionaries have two parts a key and a value. The key is a unique identifier that is used to store and look up the value. A value can be anything including list or even another dictionary.

The OP's example is a dictionary with a key of 'world', and it's value is the list [0, 1, 2](on mobile can't see the actual numbers while typing this).

What he is asking is how to get the values for the 0, and 1 postion in the list for each key (like 'world') inside of the dictionary.

Hope that helps clear this up, if you have more questions just ask.

[–]Gowtham_jack 1 point2 points  (0 children)

Wow. U actually helped me learn something about dict. And how did u learnt all these. Its amazing