[Tutor] Accessing Specific Dictionary items
Hi all, I'm trying to access and use specific items within a dictionary that is returned but am having issues in doing so. [{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408, 'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}] How can I populate a list of many different returns so that I have a list that contains all the cities and in line with the item that is referenced. The first step is to populate the list unsorted as it needs to be in correlation to the item that it came from. Thanks for your help. -- ~MEN ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accessing Specific Dictionary items
I'm not sure I'm following. Could you give an example of expected input and expected output? Sent from my Verizon Wireless BlackBerry -Original Message- From: Mike Nickey mnic...@gmail.com Sender: tutor-bounces+eire1130=gmail@python.org Date: Mon, 1 Aug 2011 15:05:30 To: tutor@python.org Subject: [Tutor] Accessing Specific Dictionary items Hi all, I'm trying to access and use specific items within a dictionary that is returned but am having issues in doing so. [{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408, 'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}] How can I populate a list of many different returns so that I have a list that contains all the cities and in line with the item that is referenced. The first step is to populate the list unsorted as it needs to be in correlation to the item that it came from. Thanks for your help. -- ~MEN ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accessing Specific Dictionary items
On 08/01/2011 03:05 PM, Mike Nickey wrote: Hi all, I'm trying to access and use specific items within a dictionary that is returned but am having issues in doing so. [{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408, 'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}] How can I populate a list of many different returns so that I have a list that contains all the cities and in line with the item that is referenced. The first step is to populate the list unsorted as it needs to be in correlation to the item that it came from. Thanks for your help. Could you give us examples of what you want the list to look like? It will help us direct you to an answer. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accessing Specific Dictionary items
The input being used is through pygeoip. Using this I am pulling the data by IP and from what I am reading this populates as a dictionary. Here is some of the output that I can show currently [{'city': 'Buena Park', 'region_name': 'CA', 'area_code': 714}, {'city': 'Wallingford', 'region_name': 'CT', 'area_code': 203}, {'city': 'Schenectady', 'region_name': 'NY', 'area_code': 518}, {'city': 'Athens', 'region_name': '35'}] I'd like to have an output similar to this: 'Buena Park', 'Wallingford', 'Schenectady','Athens' pulled by the city keys that are used in the returns. I think the easiest way to approach this would be simply to use the .append and populate a list but I don't know how to pull an item by key value from the dictionary returns. I hope this helps clear some confusion and thanks in advance. On Mon, Aug 1, 2011 at 15:14, ian douglas ian.doug...@iandouglas.com wrote: On 08/01/2011 03:05 PM, Mike Nickey wrote: Hi all, I'm trying to access and use specific items within a dictionary that is returned but am having issues in doing so. [{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408, 'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}] How can I populate a list of many different returns so that I have a list that contains all the cities and in line with the item that is referenced. The first step is to populate the list unsorted as it needs to be in correlation to the item that it came from. Thanks for your help. Could you give us examples of what you want the list to look like? It will help us direct you to an answer. -- ~MEN ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accessing Specific Dictionary items
Mike Nickey wrote: The input being used is through pygeoip. Using this I am pulling the data by IP and from what I am reading this populates as a dictionary. Here is some of the output that I can show currently [{'city': 'Buena Park', 'region_name': 'CA', 'area_code': 714}, {'city': 'Wallingford', 'region_name': 'CT', 'area_code': 203}, {'city': 'Schenectady', 'region_name': 'NY', 'area_code': 518}, {'city': 'Athens', 'region_name': '35'}] I'd like to have an output similar to this: 'Buena Park', 'Wallingford', 'Schenectady','Athens' pulled by the city keys that are used in the returns. What do you mean, the returns? If all you want is a list of cities, that's easy: cities = [d['city'] for d in list_of_dicts] or if you prefer a more verbose way: cities = [] for d in list_of_dicts: cities.append(d['city']) To get the list sorted, just sort it afterwards: cities.sort() I think the easiest way to approach this would be simply to use the .append and populate a list but I don't know how to pull an item by key value from the dictionary returns. The same way you would pull an item by key from any other dict. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accessing Specific Dictionary items
Dictionaries are objects and you access their attributes through keys. So, let's say I had a dict: d = {'city':'plattsburgh'} I would thus access the attribute by doing this d['city'] You can store that value to a variable Or you can append to a list directly. l = [] for d in yourdict: l.append(d['city']) Sent from my Verizon Wireless BlackBerry -Original Message- From: Mike Nickey mnic...@gmail.com Sender: tutor-bounces+eire1130=gmail@python.org Date: Mon, 1 Aug 2011 15:26:32 To: ian douglasian.doug...@iandouglas.com Cc: tutor@python.org Subject: Re: [Tutor] Accessing Specific Dictionary items The input being used is through pygeoip. Using this I am pulling the data by IP and from what I am reading this populates as a dictionary. Here is some of the output that I can show currently [{'city': 'Buena Park', 'region_name': 'CA', 'area_code': 714}, {'city': 'Wallingford', 'region_name': 'CT', 'area_code': 203}, {'city': 'Schenectady', 'region_name': 'NY', 'area_code': 518}, {'city': 'Athens', 'region_name': '35'}] I'd like to have an output similar to this: 'Buena Park', 'Wallingford', 'Schenectady','Athens' pulled by the city keys that are used in the returns. I think the easiest way to approach this would be simply to use the .append and populate a list but I don't know how to pull an item by key value from the dictionary returns. I hope this helps clear some confusion and thanks in advance. On Mon, Aug 1, 2011 at 15:14, ian douglas ian.doug...@iandouglas.com wrote: On 08/01/2011 03:05 PM, Mike Nickey wrote: Hi all, I'm trying to access and use specific items within a dictionary that is returned but am having issues in doing so. [{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408, 'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}] How can I populate a list of many different returns so that I have a list that contains all the cities and in line with the item that is referenced. The first step is to populate the list unsorted as it needs to be in correlation to the item that it came from. Thanks for your help. Could you give us examples of what you want the list to look like? It will help us direct you to an answer. -- ~MEN ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accessing Specific Dictionary items
Thank you all, I have found the assistance needed and the guidance here was wonderful. I needed to add a line in the middle that did a temporary storage for me before trying to append this to the list. It might not be the most elegant solution but it works. On Mon, Aug 1, 2011 at 17:28, eire1...@gmail.com wrote: Dictionaries are objects and you access their attributes through keys. So, let's say I had a dict: d = {'city':'plattsburgh'} I would thus access the attribute by doing this d['city'] You can store that value to a variable Or you can append to a list directly. l = [] for d in yourdict: l.append(d['city']) Sent from my Verizon Wireless BlackBerry -Original Message- From: Mike Nickey mnic...@gmail.com Sender: tutor-bounces+eire1130=gmail@python.org Date: Mon, 1 Aug 2011 15:26:32 To: ian douglasian.doug...@iandouglas.com Cc: tutor@python.org Subject: Re: [Tutor] Accessing Specific Dictionary items The input being used is through pygeoip. Using this I am pulling the data by IP and from what I am reading this populates as a dictionary. Here is some of the output that I can show currently [{'city': 'Buena Park', 'region_name': 'CA', 'area_code': 714}, {'city': 'Wallingford', 'region_name': 'CT', 'area_code': 203}, {'city': 'Schenectady', 'region_name': 'NY', 'area_code': 518}, {'city': 'Athens', 'region_name': '35'}] I'd like to have an output similar to this: 'Buena Park', 'Wallingford', 'Schenectady','Athens' pulled by the city keys that are used in the returns. I think the easiest way to approach this would be simply to use the .append and populate a list but I don't know how to pull an item by key value from the dictionary returns. I hope this helps clear some confusion and thanks in advance. On Mon, Aug 1, 2011 at 15:14, ian douglas ian.doug...@iandouglas.com wrote: On 08/01/2011 03:05 PM, Mike Nickey wrote: Hi all, I'm trying to access and use specific items within a dictionary that is returned but am having issues in doing so. [{'city': 'Sunnyvale', 'region_name': 'CA', 'area_code': 408, 'metro_code': 'Santa Clara, CA', 'country_name': 'United States'}] How can I populate a list of many different returns so that I have a list that contains all the cities and in line with the item that is referenced. The first step is to populate the list unsorted as it needs to be in correlation to the item that it came from. Thanks for your help. Could you give us examples of what you want the list to look like? It will help us direct you to an answer. -- ~MEN ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- ~MEN ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accessing Specific Dictionary items
On 8/1/2011 8:34 PM, Mike Nickey wrote: [snip] [{'city': 'Buena Park', 'region_name': 'CA', 'area_code': 714}, {'city': 'Wallingford', 'region_name': 'CT', 'area_code': 203}, {'city': 'Schenectady', 'region_name': 'NY', 'area_code': 518}, {'city': 'Athens', 'region_name': '35'}] IMHO this is overkill. Consider instead using City-Region as the key and storing the pther attributes in a tuple. There is no need to have separate dictionaries or lots of repetitive keys: cities = { 'Buena Park CA': (414, 'Santa Clara, CA'), 'Wallingford'CT: (203),...} -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor