Learning Python and Kodi Add-on Development - A Journey..

  Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
Roman_V_M Offline
Posts: 676
Joined: Jun 2011
Reputation: 19
Location: Kyiv, Ukraine
Post: #16
(2015-07-21 23:14)zag Wrote:  Very simple here, we are just looping though a list of groceries, and assigning each item of the list to a new variable called "food". We then print out each food item to a new line (this seems to be automatic with python).

for item in ages:
    print item, ages[item]

Here we are getting slightly more complicated by looping through a dictionary and finding the object name, and the data (in this case the age). Printing the item in brackets means it looks at the data of the dictionary, rather than the object name.

If you need to loop through both dictionary keys and values, here's more correct 'Pythonic' way:
ages = {'dad': 50, 'mum': 45, 'sister': 21}
for name, age in ages.iteritems():
    print name, age
It's worth mentioning that Python dict stores its data in random order, so there's no way to predict in which order your items will be iterated. If you need a dictionary that keeps the order in which the items have been added, collections module provides OrderedDict class that has the same properties as dict, but it keeps the order of keys.

Also please note that Python does have the official style guide for coding - PEP-8.

AMD A4-3300 + AsRock A75M-ITX
Kodi 14.x on Windows 7
(This post was last modified: 2015-07-22 11:35 by Roman_V_M.)
find quote
citislicker Offline
Junior Member
Posts: 15
Joined: Jun 2015
Reputation: 0
Post: #17
wow.. Pretty Amazing. Thank you. I am going to follow the tread and learn everything I can as well.
find quote
Post Reply