Results 1 to 2 of 2

Thread: working with iteritems() for dictionary:python

  1. #1
    Join Date
    Aug 2009
    Beans
    182

    Exclamation working with iteritems() for dictionary:python

    I've got strange error with iteritems(). why is it so?
    Code:
    >>> class test:
    	def __init__(self,**marks):
    		self._marks = marks
    	def getMarks(self):
    		for key,value in self._marks.iteritems():
    			print (self._marks(key,value))
    
    			
    >>> t1=test(sub1=99,sub2=98)
    >>> t1.getMarks()
    Traceback (most recent call last):
      File "<pyshell#12>", line 1, in <module>
        t1.getMarks()
      File "<pyshell#10>", line 5, in getMarks
        for key,value in self._marks.iteritems():
    AttributeError: 'dict' object has no attribute 'iteritems'
    but when i did items(), it works well

    Code:
    __dict = {'sub1':99,'sub2':98}
    
    >>> for key,val in __dict.items():
    	print(key,val)
    
    	
    sub2 98
    sub1 99
    Also i need to print the result in the same order as in the input i.e.
    sub1 99
    sub2 98.

    how can I do it?
    **********************
    I would like to generate a error message when someone enters a duplicate items.
    marks1 = marks(sub1 = 99, sub2 = 98, sub3 = 99)
    when again 99 is re-entered it generates an error message. how can I do it?
    __________________

  2. #2
    Join Date
    Aug 2007
    Beans
    190
    Distro
    Ubuntu

    Re: working with iteritems() for dictionary:python

    If you want to control the order of the items in your dictionary you can use OrderedDict from collections module.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •