[Tutor] list of dictionary

Patty patty at cruzio.com
Fri Feb 25 18:44:39 CET 2011


  ----- Original Message ----- 
  From: Pacific Morrowind 
  To: tutor at python.org 
  Sent: Thursday, February 24, 2011 10:21 PM
  Subject: Re: [Tutor] list of dictionary


  Hi;

  On 24/02/2011 9:35 PM, sunil tech wrote: 
    Hi all...

    i have d=[{'qty':0.0},{'qty':0.0}



  If there isn't some pressing reason to dictionaries as the list items (but since I'm not sure how you're generating the list/what you are later using the list I can't tell ofc but 

  Hi - I had just a couple comments plus clarification about lists and iterating:  

  if applicable to your situation I'd suggest just doing for creation of the list
   d = []

  I like creating the empty variable structure, makes the code really clear. 

  (logic for whatever gives your values)
  d.append(value)
  etc.)

    when all the qty is 0.0,
    i want to perform some print operation
    (only at once, after it checks everything in the list of dictionary 'd')...

    if its not 0.0,
    print some message...

    Thank you in advance

  Presuming you do have to use the dictionaries:
  qty = 0.0
  for item in d:

  Right here - is the variable 'item' created right on the spot to iterate over this list?  And I think you are doing the same thing when you create the variable 'subitem' in the line below, right?  I am  trying to get myself to recognize an iterator variable as opposed to a counter variable I create myself  to keep track of these things (used in other programming languages) - and realizing the difference between counter/iterator variables and variables that I really care about like 
   'd = []' .

  Thanks!

  Patty


      for subitem in d:
          if item[subitem] != 0.0:
              qty = item[subitem]
              break
  if qty != 0.0:
      print "some message - for example a non zero qty = %f" % qty

  (presuming you are using Python 2x - if using python 3x the syntax will be slightly different)
  Hopefully that will serve your purpose if not just post again and I or one of the more frequently posting helpful users here will answer soon.
  Nick




------------------------------------------------------------------------------


  _______________________________________________
  Tutor maillist  -  Tutor at python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110225/a446bd32/attachment-0001.html>


More information about the Tutor mailing list