-1

My concern here is if name_3[0]>50, add it to html table and if name_3[0]<=50, discard it and iterate to next value in dictionary because I don't want to add entry below 50. Below is the logic which I think of. But can we iterate through next value ?

for name_1 in sorted(any_dictionary.keys()):
    for name_2 in sorted(any_dictionary[name_1].keys()):
        for name_3 in any_dictionary[name_1][name_2]:
             if (name_3[0] > 50):
                    size_KB = name_3[0]
             address=name_3[1]
             html += """<tr>
                   <td>{}</td>
                   <td>{}</td>
                   <td>{:,}</td>
                   <td>{:,} [{}]</td>
                   <td>{}</td>
                   </tr>\n""".format(name_1,
                                     name_2,
                                     size_KB,
                                     name_3[2],
                                     name_3[3],
                                     address)
Jay
  • 13
  • 1
  • 6
  • 2
    You iterate over keys, not values. It's more like: `for key in any_dictionary` – sisanared Nov 17 '16 at 16:46
  • Dicts are not ordered. If you remove an element, there's no guarantee of what will happen. https://docs.python.org/3/library/stdtypes.html#typesmapping – Goodies Nov 17 '16 at 16:46
  • http://stackoverflow.com/questions/5384914/how-to-delete-items-from-a-dictionary-while-iterating-over-it – Chris Koston Nov 17 '16 at 16:47
  • Possible duplicate of [Best way to remove an item from a Python dictionary?](http://stackoverflow.com/questions/5447494/best-way-to-remove-an-item-from-a-python-dictionary) – Bahrom Nov 17 '16 at 16:47
  • The thing is I don't want to remove it, but if (value[0]>50) I have to use it further and if (value[0]<=50), move to next value in dictionary @Bahrom – Jay Nov 17 '16 at 17:08
  • Some issues with the code you posted. As mentioned, Python iterates keys by default, so if you want values try `for value in any_dictionary,values()` or `for _, value in any_dictionary.items()`. Second, `print size_KB` will throw a "variable referenced before assignment" error if the first conditional clause is False. – pylang Nov 17 '16 at 19:11
  • @Bahrom edited the question. How I can only include elements to html table if its >50 – Jay Nov 18 '16 at 18:04
  • @Moinuddin Quadri can you please help me with this question? I got stuck here – Jay Nov 18 '16 at 18:25

2 Answers2

0

As far as I know, it is never a good idea to change a dictionary (or list or some set) while iterating over it.

Maybe add those values, which you want to keep, in a new dictionary instead?

0

As per your last comment:

  • Iterate over the values in the dictionary
  • Only assign size_KB and operate on it if the first entry in value is greater than 50.

i.e (you don't need the else block):

>>> size_KB = None
>>> d = {'a': [40], 'b': [50], 'c': [100], 'e': [30], 'f': [200]}
>>> for value in d.values():
...     if (value[0] > 50):
...         size_KB = value[0]
...     print 'Value is:', value
...     print 'Size is:', size_KB 
...     
Value is: [40]
Size is: None
Value is: [100]
Size is: 100
Value is: [50]
Size is: 100
Value is: [30]
Size is: 100
Value is: [200]
Size is: 200
>>> 

Size is only getting assigned when value[0] is greater than 50.

For your html:

>>> d = {'a': [40], 'b': [50], 'c': [100], 'e': [30], 'f': [200]}
>>> row_template = '<tr><td>%s</td></tr>'
>>> html = '<table style="width:100%">' + '\n'.join([row_template % value[0] for value in d.values() if value[0] > 50]) + '</table>'
>>> html
'<table style="width:100%"><tr><td>100</td></tr>\n<tr><td>200</td></tr></table>'
Bahrom
  • 4,752
  • 32
  • 41
  • @Darsh that's the whole point of iterating over the values. The loop will move on to the next element. If value[0] is greater than 50, it will execute the body of the if statement. Otherwise it will just move on to the next value. – Bahrom Nov 17 '16 at 17:35
  • Yes, you unindented the print. It should remain inside the IF so that it prints if the statement is True. Otherwise the loop will continue to the next item. – Bahrom Nov 17 '16 at 17:52
  • I did it purposely. I want to use the size_KB for further use. Outside of if statement – Jay Nov 17 '16 at 17:58
  • Actually, I am adding size_KB to html table. So if size_KB=None, I don't want to add it. I only want to add size_KB to html table if its >50 – Jay Nov 17 '16 at 18:43
  • What datatype is your html table? Just a string? Can you post it here? – Bahrom Nov 17 '16 at 18:45
  • Datatype of html table is string – Jay Nov 17 '16 at 18:48