Get key from dictionary

I just started with Python,

Is there some iteration in dictionary like in PHP

foreach(aData as key=>value)

-------------Problems Reply------------

It looks like something like this :

my_dict = {"key1": 1, "key2":2}
my_dict.items() # in python < 3 , you should use iteritems()
>>> ("key1", 1), ("key2", 2)

so you can iterate on it :

for key, value in my_dict.items():
do_the_stuff(key, value)

assuming python 2:

for key, value in aData.iteritems():

Use:

for key in dictionary.keys()
value = dictionary[key]
#do something

OR:

for key,value in dictionary.items():
#do something

Category:python Views:0 Time:2011-06-29

Related post

  • How to get the list of key in dictionary 2009-08-14

    I never gotten any code I tried to work?. I want to key and not value(yet). Using another array prove to be much work as I use remove also. --------------Solutions------------- List<string> keyList = new List<string>(this.yourDictionary.K

  • how to fetch keys of dictionary in C# 2009-12-04

    I'm using a Dictionary<char, ulong> where the char is a obj no, & ulong is offset. Now, I need to access the Keys of Dictionary. Keys is any random number which cant predict. I am using VS-2005. I am new to C# so plz provide some code. ----

  • how to add duplicate keys to dictionary 2010-06-14

    how to add duplicate keys to dictionary i.e i have already added the key,value pair as rollno,1 but i need to add the same parameter to the dictionary,but it is not allowing to add. how to add duplicated keys/repeated key in dictionary . or any other

  • Using object property for a key in dictionary 2010-10-29

    I would like to use an objects property as the key for a dictionary. Can this be done? The ultimate goal for this is to use this so can see if the property is locked or not, in various states that an object can be in. These locked value is not persis

  • Object as key in Dictionary 2011-01-17

    I have this class: class A { string name; string code; } Examples: A1: name="blabla", code="kuku" A2: name="blabla", code=null A3: name=null, code="kuku" Dictionary<A, string> d=new Dictionary<A, string>(); d[A1]="aaa"; d[A2]="bbb" result

  • How to find minimum key in dictionary 2011-01-26

    I declare dictionary as following: private Dictionary<int, touchInformation> touchDictionary = new Dictionary<int, touchInformation>(); And I used as following: touchDictionary[touchID] = touchObject; So, the touchDictionary will keep the

  • The type initializer for 'System.Web.MimeMapping' threw an exception: Item has already been added. Key in dictionary: '.fif' Key being added: '.gif' 2011-04-13

    I've never seen anything like this before. Tried everything I could, searched the web with no luck and in the end I have absolutely no idea what's going on. I bet this is going to be one of those silly easy to fix bugs, although I don't quite get the

  • error on sitecore content editor "Item has already been added. Key in dictionary: 'treelist' Key being added: 'treelist' 2012-02-15

    We restarted our app pool and trying to login to content editor of sitecore, we are gettting the below error as in teh screen shot. Item has already been added. Key in dictionary: 'treelist' Key being added: 'treelist' http://screencast.com/t/qo8CWoj

  • asp.net :is time complexity of searching an value by key in dictionary constant time or log2(n)? 2009-05-21

    i need a datastructure in dotnet where i can search an item in constant time.it means that datastructure should implement indexing internally.is dictionary usefull for this purpose or some other one? --------------Solutions------------- Yes, use Dict

  • Does the length of key affect Dictionary performance? 2009-10-08

    I will use a Dictionary in a .NET project to store a large number of objects. Therefore I decided to use a GUID-string as a key, to ensure unique keys for each object. Does a large key such as a GUID (or even larger ones) decrease the performance of

  • Custom Class used as key in Dictionary but key not found 2009-10-27

    I have a class, show below, which is used as a key in a Dictionary<ValuesAandB, string> I'm having issues when trying to find any key within this dictionary, it never finds it at all. As you can see, I have overridden Equals and GetHashCode. To

  • Why if hashCode method is implemented, equals methods must also be implemented in case of keys in Dictionary the datatype? 2009-10-28

    Datatype: Dictionary keys Can somebody please tell me importance of implementing both(hashCode/equals) of them simultaneously. because I think if we implement hashCode method equals is going to compare hashCodes and give us the equality. ------------

  • C#: get first key from Dictionary 2009-11-30

    I'm using a System.Collections.Generic.Dictionary<string, string>. I want to return the first key from this dictionary. I tried dic.Keys[0] but the only thing I have on the Keys property (which is a KeyCollection object) is an enumerator. Do I

  • Creating anonymous class as custom key in dictionary 2010-01-29

    While using dictionary, i always override GetHashCode and Equals ( or provide a custom comparer to the dictionary). What happens behind the covers when i create an anonymous class as key? Sample Code.... var groups=(from item in items group item by n

  • How to get the value for each key in dictionary in Objective-C? 2010-02-18

    I'm maintaining a NSMutableDictionary which holds key and value pair.Now i need to perform some operation for each value in it.How to retrive value from dictionary. // this is NSMutableDIctionary NSMutableDictionary *dictobj = [[NSMutableDictionary a

  • Sort by key of dictionary inside a dictionary in Python 2010-03-10

    How to sort the following dictionary by the value of "remaining_pcs" or "discount_ratio"? promotion_items = { 'one': {'remaining_pcs': 100, 'discount_ratio': 10}, 'two': {'remaining_pcs': 200, 'discount_ratio': 20}, } EDIT What I mean is getting a so

  • Composite key in Dictionary; override GetHashCode(), Equals etc or use structs? 2010-03-15

    I have quite a few dictionaries where the key is a composite of several different values (mostly strings and integers). Do I implement these keys as classes (and override GetHashCode(), Equals() etc) or do I use struct instead? ReSharper makes it eas

  • Object pointer value as key into dictionary 2010-04-25

    I want to use the object's reference value as a key into a dictionary, as opposed to a copy of value of the object. So, I essentially want to store an object associated with a particular instance of another object in a dictionary and retrieve that va

  • vb.net - Using arraylist as key in dictionary 2010-05-12

    Dim dct As New Dictionary(Of ArrayList, ArrayList) ' Populate Dictionary dct.Add(New ArrayList({"Dot", "0"}), New ArrayList({20, 30, 40,> 50})) dct.Add(New ArrayList({"Dot", "1"}), New ArrayList({120, 130, 140,> 150})) ' Search in dictionary Di

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.134 (s). 11 q(s)