How do I iterate over dictionary list positions in python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Hi I have a dictionary where the values are lists.

position = {"a" :[0,0], "b":[0,100], "c":[50,100], "d": [50, 0]}

I m inserting each dictionary value into a handler.

poly_pos = [position["a"], position["b"], position["c"], position["d"]]

I want to iterate over the dictionary list and add value to the 0th element of each list position.

For instance for position["a"][0] I want to add 50, and for every subsequent ["letter"][0] element of each list in the dictionary I want to add 50.

If this isn t the best way to solve this, I m curious to know other ways I can solve, maybe without using dictionary value pairs.

Answers

position.values() gives the same thing as your definition of poly_pos:

for p in position.values():
    p[0] += 50

You can also use position.itervalues() to avoid creating the list if you re using Python 2.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/23393230/how-do-i-iterate-over-dictionary-list-positions-in-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils