0

I have a dict inside a class. Trying to print, delete, te.is_empty() # Not printing anything te.printstats() # not printing my stack. Nothing.

   for k,v in te.items():
      if v[0] == 'Available':
          print(k) 

Can you help me?

YouHaveaBigEgo
  • 220
  • 6
  • 13

2 Answers2

1

There are a few mistakes in your code, for example -

te.is_empty() # Not printing anything

Obviously, you are missing a print statement, do this -

print(te.is_empty())

And here -

for k,v in te.items():

I don't know why you are not getting an error here but it should be something like -

for k, v in te.items.items():

Why? Because your instance contains items that you need to iterate with python's items as in your __init__, it's a dict. So essentially, modify your code to -

# for k, v in te.items.iteritems(): # python2
for k, v in te.items.items(): #python3, this is what you need
    if "Available" in v:
        print(k)

And your prints should work

Sushant
  • 3,499
  • 3
  • 17
  • 34
  • I see `te.printstats()` didn't work for me because of the missing prints. For the second modification to print 'Available' tests, I still dont get output after copy/paste of your suggestion above. – YouHaveaBigEgo Jul 24 '18 at 20:39
  • Okay great, no problem @YouHaveaBigEgo – Sushant Jul 24 '18 at 20:42
  • Can you also help me how to delete a key/list pair? if test say is `Test4`? Is my pop method correct in my class? – YouHaveaBigEgo Jul 24 '18 at 20:45
  • @YouHaveaBigEgo did you try the pop method? Did it remove the item? If not, we'll debug it – Sushant Jul 24 '18 at 20:46
  • I get the error "RuntimeError: dictionary changed size during iteration" When I try : for k, v in te.items.items(): if v[0] == 'Running' and v[1] == 'Pending': print(k) te.remove(k) – YouHaveaBigEgo Jul 24 '18 at 21:37
  • How do I avoid this error , it still doesn't work for me. I changed my method back to self.items.pop(item) in my class – YouHaveaBigEgo Jul 24 '18 at 21:49
  • You'll need to build a temporary dictionary of items that you want to pop. After the iteration is done, you can pop. Refer [here](https://stackoverflow.com/questions/5384914/how-to-delete-items-from-a-dictionary-while-iterating-over-it) – Sushant Jul 24 '18 at 21:56
  • The solution in that link is for Python 2. How do I do on Python 3? – YouHaveaBigEgo Jul 24 '18 at 23:46
1

I'm not sure I 100% understand your question, but most of the problems you seem to be having are due to these issues in your code:

  1. The is_empty() and printstats() calls don't print anything because you don't call the print() function. The two functions only return True/False and the stack's items respectively. If you intend to print these values, you'll need to surround the calls with print() functions.
print(te.is_empty())
  1. This isn't working because you're calling the te_stack object's items field as if it were a function. I think the confusion is happening here because te.items is a dictionary, which itself has an items() method, which returns the list of tuples I assume you want (basically). So, the line should read:
for k, v in te.items.items():
  1. With #1 in mind, I think this works as intended.
Sam Holloway
  • 121
  • 3
  • How do I edit a value inside my list? Say if my value for a test says it's 'Pending', I would like to modify it to 'Pass' – YouHaveaBigEgo Jul 24 '18 at 20:41
  • The way to do this with the way your code is currently written is: `te.items['Test6'][1] = 'Pass'` However, it might be beneficial for your to design a "Test" data structure that contains fields for running status and pass/fail status and methods to manipulate those. – Sam Holloway Jul 24 '18 at 20:44
  • @YouHaveaBigEgo I think first you'll need to get the key that you want to modify and write a small logic to modify it. Just use the for loop logic and loop over the list – Sushant Jul 24 '18 at 20:45
  • Is the `remove` method in my class correct way to remove a key/value? – YouHaveaBigEgo Jul 24 '18 at 20:50
  • Yes, the dictionary's `pop()` method works (it did when I tested your code). Alternatively, you can do `del(self.items[arg])`. The difference is that `pop()` returns the deleted argument, `del()` does not. – Sam Holloway Jul 24 '18 at 21:00
  • It didnt for me. I did `if k == 'Running' and "Pending" in v: te.remove(k)`.I forgot to tell you that I added another record to my dict. with value `Pending` – YouHaveaBigEgo Jul 24 '18 at 21:04
  • @sgh304 : I can't directly access `self.items`. I did : `for k, v in te.items.items(): if k == 'Running' and v[1] == 'Pending': te.remove(k)` . Remove isn't working still – YouHaveaBigEgo Jul 24 '18 at 21:12
  • @sgh304. Can you check if this remove method works? def del_item(obj): for k, v in te.items.items(): if k == 'Running' and v[1] == 'Pending': te.remove(k) – YouHaveaBigEgo Jul 24 '18 at 21:20
  • @sgh304: typo above. it should read v[0] == 'Running' and v[1] == 'Pending'. I get the error RuntimeError: dictionary changed size during iteration – YouHaveaBigEgo Jul 24 '18 at 21:50