0

I'm learning python and I'm trying to use a dictionary (or any other way) to setup a "list dictionary" that I can iterate with a for loop.

Here's and example of the data structure that I need to loop through:

name: alert1
id: 12345
name: alert2
id: 54321

I've got it working with the code below, but I'm not sure I'm doing the "recommended" way, or if there are any big flaws with the code:

# Gets arguments
option = (sys.argv[1])
def disable_alert(alert_name,alert_id):
 disable_alert = {'enabled':False,'dampeningCategory':'ONCE'}
 print "Disabling alert %s (ID: %s)" % (alert_name,alert_id)
 req = requests.put(endpoint+'alert/definition/%s' % (alert_id),json.dumps(disable_alert),headers=headers,auth=auth)
 check_status = req.json()['enabled']
 if check_status == False:
 print "Alert %s disabled\n" % alert_name
 else:
 print "Alert %s did not disable\n" % alert_name
alerts = {'name':['ils.txdatasource.dbpool','SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE','SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE'],'id':['10435','10423','10421']}
if option == "disable":
 count = 0
 for nothing in alerts['name']:
 disable_alert(alerts['name'][count],alerts['id'][count])
 count = count - 1
else:
 print "I don't know that option"

Here's an example output of the working code:

$ python jon_alerts.py disable
Disabling alert ils.txdatasource.dbpool (ID: 10435)
Alert ils.txdatasource.dbpool disabled
Disabling alert SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE (ID: 10423)
Alert SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE disabled
Disabling alert SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE (ID: 10421)
Alert SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE disabled
asked Feb 10, 2015 at 19:05
4
  • i think its fine but you could make it better by doing for name,id in zip(alerts["name"],alerts["id"]): print name,id Commented Feb 10, 2015 at 19:07
  • stackoverflow.com/questions/6332691/python-dictionary-iteration Commented Feb 10, 2015 at 19:08
  • You can iterate a dictionary like a list but unlike a list a dictionary is not sorted. If the order is not important there is nothing wrong. Commented Feb 10, 2015 at 19:08
  • 2
    probably a better question for codereview.stackexchange.com Commented Feb 10, 2015 at 19:18

1 Answer 1

1

You can iterate through a python dictionary. I think a better way to structure the information, rather than

alerts = {'names': [some list of names], 'ids': [some list of ids]}

might be this:

alerts = {
 'ils.txdatasource.dbpool': '10435',
 'SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE': '10423',
 'SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE': '10421',
 'alert4': 'id4',
 ...
}

Then you would just iterate like this:

for name in alerts:
 disable_alert(name, alerts[name])

No need for counters or anything like that. Usually, if you find yourself wanting to use a counter in python, there's probably a better way to do it than actually using a counter.

Just to show you how that dictionary would be accessed, I just did this really quickly at a python command line:

>>> alerts = {'ils.txdatasource.dbpool': '10435', 'SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE': '10423', 'SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE': '10421', 'alert4': 'id4'}
>>> for name in alerts:
... print 'name: {0}, id: {1}'.format(name, alerts[name])
... 
name: ils.txdatasource.dbpool, id: 10435
name: SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE, id: 10423
name: alert4, id: id4
name: SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE, id: 10421
>>>

Notice it didn't go through the items in the same order that I declared them in a dictionary. Dictionaries are unordered. However, it doesn't seem like you'd need them to go through in order for this use case.

answered Feb 10, 2015 at 19:26
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, for both the explanation and the example. I changed the script to use the simple dictionary as you described as it's a lot cleaner.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.