How do you print a key of a dictionary in Python? - python

Suppose I have a dictionary:
dictionary1 = {'test':'output','test2':'output2'}
How would I be able to print the key, test, on the screen?
I only want one of the keys at a time, not all of them.
By the way, not literally, by doing print('test'), I mean how do you print the key of any dictionary?
Like is there something like this:
#pseudocode
x = dictionary1.keys()[0]
>>> print(x)
'test'
I do not want to sort the dictionary that I'm actually using in my program.

A dictionary may have any number of keys, 0+. To print them all (if any) in sorted order, you could
for key in sorted(dictionary1):
print(key)
If you don't care about the order (i.e you're fine with a random-looking order), remove the sorted call.
If you want to be selective (e.g only print keys that are sequences of length 4, so that in your example test would be printed but test2 wouldn't):
for key in sorted(dictionary1):
try:
if len(key) == 4:
print(key)
except TypeError:
pass
If you want to print only one such key, add a break after the print.
There are at least 347 other things you could mean by your extremely vague question, but I tried to field the ones that seem most likely... if that's not enough for you, edit your question to make it much more precise!-)
Added: the edit only said the OP wants to print only one key and do no sorting, so it remains a mystery how that one key is to be picked (I suspect there's a misunderstanding -- as if dict keys had a specific order, which of course in Python they don't, and the OP wants "the first one", which just can't be pinned down -- OrderedDict is a very different [and alas inevitably slower] beast, but dicts what the OP's showing).
So if the goal is to print "any one key, no matter which one" (and it is known that thedict is not empty):
print(next(iter(thedict)))
is the simplest, most direct way -- a dict is iterable, yielding its keys in arbitrary order; iter returns an iterator on its iterable argument; and next returns the first (first in arbitrary order means an arbitrary one of course) item of its iterator argument.
If it's possible for thedict to be empty, and nothing must be printed in that case, just add a guard:
if thedict: print(next(iter(thedict)))
This will print nothing for an empty dictionary, the only key for a dictionary with length 1, an arbitrary key for a dictionary with length greater than 1.

If you're trying to find a key associated with a specific value you can do
keys = [key for key in d if d[key] == value]
print(keys)

Related

Getting keys and values of dictionary if key in list

So I have a dictionary names "ngrams_count". I want to find all keys in this dictionary that are in a list called "words_to_find". I would also like to return the values associated with those keys.
So far, this is what I'm working with
ideasrep = [key for key in words_to_find if key in ngrams_count]
That returns only the keys that are found in the word list.
I'm also looking for a way to return only the key/values pairs for which the value is greater than one. I've tried a similar technique as this:
[(key,values) for key, values in ngrams_count.items() if values > 1]
However, this only seems to work if I stay within the dictionary and I'm running out of ideas... Ideally, I'd like a way to do these two things simultaneously.
Your first version is almost right, you just need to add ngrams_count[key] to the result.
ideasrep = [(key, ngrams_count[key]) for key in words_to_find if key in ngrams_count]
Or you can use the second version, but change the condition to check if the key is in words_to_find.
[(key,values) for key, values in ngrams_count.items() if key in words_to_find]
If words_to_find is big, you should convert it to a set before the list comprehension to make the second version more efficient.

Using a for loop to print keys and/or values in a dictionary for python. Looking for logical thinking explanation thanks :D

My problem is understanding why these certain lines of code do what they do. Basically why it works logically. I am using PyCharm python 3 I think.
house_Number = {
"Luca": 1, "David": 2, "Alex": 3, "Kaden": 4, "Kian": 5
}
for item in house_Number:
print(house_Number[item]) # Why does this print the values tied with the key?
print(item) # Why does this print the key?
This is my first question so sorry I don't know how to format the code to make it look nice. My question is why when you use the for loop to print the dictionary key or value the syntax to print the key is to print every item? And what does it even mean to print(house_Number[item]).
They both work to print key or value but I really want to know a logical answer as to why it works this way. Thanks :D
I'm not working on any projects just starting to learn off of codeacademey.
In Python, iteration over a dictionary (for item in dict) is defined as iteration over that dictionary's keys. This is simply how the language was designed -- other languages and collection classes do it differently, iterating, for example, over key-value tuples, templated Pair<X,Y> objects, or what have you.
house_Number[item] accesses the value in house_Number referenced by the key item. [...] is the syntax for indexing in Python (and most other languages); an_array[2] gives the third element of an_array and house_Number[item] gives the value corresponding to the key item in the dictionary house_Number.
Just a side note: Python naming conventions would dictate house_number, not house_Number. Capital letters are generally only used in CamelCasedClassNames and CONSTANTS.
In python values inside a dictionary object are accessed using dictionay_name['KEY']
In your case you are iterating over the keys of dictionary
Hope this helps
for item in dic:
print(item) # key
print(dic[item]) # value
Dictionaries are basically containers containing some items (keys) which are stored by hashing method. These keys just map to the values (dic[key]).
Like in set, if you traverse using for loop, you get the keys from it (in random order since they are hashed). Similarly, dictionaries are just sets with a value associated with it. it makes more sense to iterate the keys as in sets (too in random order).
Read more about dicionaries here https://docs.python.org/3/tutorial/datastructures.html#dictionaries and hopefully that will answer your question. Specifically, look at the .items() method of the dictionary object.
When you type for item in house_Number, you don’t specify whether item is the key or value of house_Number. Then python just thinks that you meant the key of house_Number.
So when you do the function print(house_Number[item]), you’re printing the value because your taking the key and finding the value. In other words, you taking each key once, and finding their values, which are 1, 2, 3, 4, 5, 6
The print(item) is just to print the item, which are the keys, "Luca", "David", "Alex", "Kaden", "Kian"
Because the print(house_Number[item]) and print(item) alternating, you get the keys and values alternating, each on a new line.

In-place Python dictionary inverse

The task that I wanted to see if possible to solve is, swapping key,value pairs of a dictionary (in Python), with an in-place calculation, without additional data-structures (Only a constant number of extra variables). It seems rather impossible (in a finite world), but I'm open to hear suggestions on solving it.
I've seen a few posts about in-place dictionary inverse in python, and I've found one common thing between all of the solutions.
The following dictionary won't be properly inversed:
dict = {'b':'a','a':'c','c':123}
The reason for that is, when swapping the first argument, we overwrite 'a''s actual value (The values are unique, the keys are unique, but that doesn't mean there isn't a value that is the same as an already existing key)
NOTES:
1) The dictionary given as an example has hashable values.
2) The key/values can be of any data-type. Not necessarily strings.
I'd love to hear ways to solve it, I've thought of one but it only works if we have infinite memory, which obviously is not true.
EDIT:
1) My Idea was, changing the dictionary such that I add a constant number of underscores ("_") to the beginning of each key entry. The number of underscores is determined based on the keys, if some key has X underscores, I'll add X+1 underscores (max_underscores_of_key_in_prefix+1).
To work around objects in the keys, I'll make a wrapper class for that.
I have tried my best explaining my intuition, but I am not sure this is practical.
2) #Mark Ransom's solution works perfectly, but if anyone has an other algorithmic solution to the problem, I'd still love to hear it out!
I mark this question as solved because it is solved, but again, other solutions are more than welcome :-)
Obviously for this to be possible, both keys and values must be hashable. This means that none of your keys or values can be a list. We can take advantage of this to know which dictionary elements have been already processed.
Since you can't iterate and modify a dictionary at the same time, we must start over every time we swap a key/value. That makes this very slow, an O(n^2) operation.
def invert_dict(d):
done = False
while not done:
done = True
for key, val in d.items():
if isinstance(val, list):
if len(val) > 1:
d[key] = [val[1]]
val = val[0]
else:
del d[key]
if not isinstance(val, list):
if val in d:
d[val] = [d[val], key]
else:
d[val] = [key]
done = False
break
for key, val in d.items():
d[key] = val[0]

Python: To avoid a TypeError do I need to check if there is a list in a dictionary, then check if that list holds the item?

I have several dictionaries. Sometimes they have with the key thing whose value is a dictionary.
Sometimes that dictionary has the key anotherthing whose value is a string which could be empty. So if I want to check the value of anotherthing to see if A. The key actually exists and B. the value evaluates to something, I need to use three if statements (because any one of the keys could not exist)?
if 'thing' in dictionary_one:
if 'anotherthing' in dictionary_one['thing']:
val = dictionary_one['thing']['anotherthing']
if val:
# Do something with value
In other languages a value will just evaluate to None is there a way around this other than using a Try...Except?
You could chain the lookup with dict.get():
val = dictionary_one.get('thing', {}).get('anotherthing')
if val:

How to work around needing to update a dictionary

I need to delete a k/v pair from a dictionary in a loop. After getting RuntimeError: dictionary changed size during iteration I pickled the dictionary after deleting the k/v and in one of the outer loops I try to reopen the newly pickled/updated dictionary. However, as many of you will probably know-I get the same error-I think when it reaches the top of the loop. I do not use my dictionary in the outermost loop.
So my question is-does anyone know how to get around this problem? I want to delete a k/V pair from a dictionary and use that resized dictionary on the next iteration of the loop.
to focus the problem and use the solution from Cygil
list=[27,29,23,30,3,5,40]
testDict={}
for x in range(25):
tempDict={}
tempDict['xsquared']=x*x
tempDict['xinverse']=1.0/(x+1.0)
testDict[(x,x+1)]=tempDict
for item in list:
print 'the Dictionary now has',len(testDict.keys()), ' keys'
for key in testDict.keys():
if key[0]==item:
del testDict[key]
I am doing this because I have to have some research assistants compare some observations from two data sets that could not be matched because of name variants. The idea is to throw up a name from one data set (say set A) and then based on a key match find all the names attached to that key in the other dataset (set B). One a match has been identified I don't want to show the value from B again to speed things up for them. Because there are 6,000 observations I also don't want them to have to start at the beginning of A each time they get back to work. However, I can fix that by letting them chose to enter the last key from A they worked with. But I really need to reduce B once the match has been identified
Without code, I'm assuming you're writing something like:
for key in dict:
if check_condition(dict[key]):
del dict[key]
If so, you can write
for key in list(dict.keys()):
if key in dict and check_condition(dict[key]):
del dict[key]
list(dict.keys()) returns a copy of the keys, not a view, which makes it possible to delete from the dictionary (you are iterating through a copy of the keys, not the keys in the dictionary itself, in this case.)
Delete all keys whose value is > 15:
for k in mydict.keys(): # makes a list of the keys and iterate
# over the list, not over the dict.
if mydict[k] > 15:
del mydict[k]
Change:
for ansSeries in notmatched:
To:
for ansSeries in notmatched.copy():

Categories