go through a dictionary picking keys from it in a loop?
For example lets say I have the following dictionary: {'hello':'world', 'hi':'there'}. Is there a way to for loop through the dictionary and print hello, hi?
on a similar note is there a way to say myDictionary.key[1] and that will return hi?
You can iterate over the keys of a dict with a for loop:
>>> for key in yourdict:
>>> print(key)
hi
hello
If you want them as a comma separated string you can use ', '.join.
>>> print(', '.join(yourdict))
hi, hello
on a similar note is there a way to say myDictionary.key1 and that will return hi
No. The keys in a dictionary are not in any particular order. The order that you see when you iterate over them may not be the same as the order you inserted them into the dictionary, and also the order could in theory change when you add or remove items.
if you need an ordered collection you might want to consider using another type such as a list, or an OrderedDict
You can use the .keys() method:
for key in myDictionary.keys():
print(key)
You can also use .items() to iterate through both at the same time:
for key, value in myDictionary.items():
print(key, value)
Using the dictionary name as a sequence produces all the keys:
>>> d={'hello':'world', 'hi':'there'}
>>> list(d)
['hi', 'hello']
so list({'hello':'world', 'hi':'there'})[1] produces element 1 of the list of keys.
This is of limited use, however, because dictionaries are unordered. And their order may be different than the order of insertion:
>>> d={'a': 'ahh', 'b': 'baa', 'c': 'coconut'}
>>> d
{'a': 'ahh', 'c': 'coconut', 'b': 'baa'}
You can do sorted(list({'hello':'world', 'hi':'there'}))[1] for the 1 element of a sorted list of the keys of your dict. That produces 'hi' in this case. Not the most readable or efficient though...
You should look at OrderedDict if you want a sorted order.
Or just sort into a list:
>>> d={'a': 'ahh', 'b': 'baa', 'c': 'coconut'}
>>> l=[(k,v) for k, v in d.items()]
>>> l.sort()
>>> l[1]
('b', 'baa')
>>> l[1][0]
'b'
You can reverse (k,v) to (v,k) if you want to sort by value instead of by key.
dict.iterkeys in Python 2, dict.keys in Python 3.
d = { 'hello': 'world', 'hi': 'there' }
for key in d.iterkeys():
print key
Sounds like a list of keys would meet your needs:
>>> d = { 'hello': 'world', 'hi': 'there' }
>>> keys = list(d)
>>> keys
['hi', 'hello']
>>> from random import choice
>>> choice(keys)
'hi'
Related
With three list variables (all different sizes) and a list comprehension:
indexesA=[1,2,3,4,5]
indexesB=['a','b','c','d']
values=['Dog','Cat','Sheep','Donkey','Horse']
keys=[2,3,'d']
print [values[indexesA.index(key)] for key in keys if key in indexesA and indexesA.index(key)<len(values)]
I need to get a dictionary:
{2: 'Cat', 3: 'Sheep', 'd': 'Donkey'}
indexesA list should be checked if key among its values first. If not then check indexesB.
How to achieve this?
Edited:
Please note that the list comprehension I posted in my question is only checking indexesA for the key. The code is incomplete.
print [values[indexesA.index(key)] for key in keys if key in indexesA and indexesA.index(key)<len(values)]
This is one way to do it using list-comprehensions
>>> keys = [2, 3, 'd']
>>> {k: values[indexesA.index(k) if k in indexesA else indexesB.index(k)] for k in keys if (k in indexesA) or (k in indexesB)}
{2: 'Cat', 3: 'Sheep', 'd': 'Donkey'}
>>>
Disclaimer: This code is neither readable, maintainable or extendable.
I'm working on an assignment. Is there anyway a dictionary can have duplicate keys and hold the same or different values. Here is an example of what i'm trying to do:
dict = {
'Key1' : 'Helo', 'World'
'Key1' : 'Helo'
'Key1' : 'Helo', 'World'
}
I tried doing this but when I associate any value to key1, it gets added to the same key1.
Is this possible with a dictionary? If not what other data structure I can use to implement this process?
Use dictionaries of lists to hold multiple values.
One way to have multiple values to a key is to use a dictionary of lists.
x = { 'Key1' : ['Hello', 'World'],
'Key2' : ['Howdy', 'Neighbor'],
'Key3' : ['Hey', 'Dude']
}
To get the list you want (or make a new one), I recommend using setdefault.
my_list = x.setdefault(key, [])
Example:
>>> x = {}
>>> x['abc'] = [1,2,3,4]
>>> x
{'abc': [1, 2, 3, 4]}
>>> x.setdefault('xyz', [])
[]
>>> x.setdefault('abc', [])
[1, 2, 3, 4]
>>> x
{'xyz': [], 'abc': [1, 2, 3, 4]}
Using defaultdict for the same functionality
To make this even easier, the collections module has a defaultdict object that simplifies this. Just pass it a constructor/factory.
from collections import defaultdict
x = defaultdict(list)
x['key1'].append(12)
x['key1'].append(13)
You can also use dictionaries of dictionaries or even dictionaries of sets.
>>> from collections import defaultdict
>>> dd = defaultdict(dict)
>>> dd
defaultdict(<type 'dict'>, {})
>>> dd['x']['a'] = 23
>>> dd
defaultdict(<type 'dict'>, {'x': {'a': 23}})
>>> dd['x']['b'] = 46
>>> dd['y']['a'] = 12
>>> dd
defaultdict(<type 'dict'>, {'y': {'a': 12}, 'x': {'a': 23, 'b': 46}})
I think you want collections.defaultdict:
from collections import defaultdict
d = defaultdict(list)
list_of_values = [['Hello', 'World'], 'Hello', ['Hello', 'World']]
for v in list_of_values:
d['Key1'].append(v)
print d
This will deal with duplicate keys, and instead of overwriting the key, it will append something to that list of values.
Keys are unique to the data. Consider using some other value for a key or consider using a different data structure to hold this data.
for example:
don't use a persons address as a unique key because several people might live there.
a person's social security number or a drivers license is a much better unique id of a person.
you can create your own id to force it to be unique.
I've been trying to remove a specific item from multiple values in a dictionary in python and am not completely sure how to do this. For example, in the dictionary:
d = {('hello', 'hi', 'how are you'): 2, ('hi', 'hello', 'how are you'): 1}
How would I remove 'hi' so that all that remains is:
d = {('hello', 'how are you'): 2, ('hello', 'how are you'): 1}
You apparently want to change the key. So you simply need to store the corresponding value with the new key and remove the old one. However, in your case creating a new dict is easier since you want to modify every single item.
d2 = {}
for key, value in d.iteritems():
key = list(key)
if 'hi' in key:
key.remove('hi')
d2[tuple(key)] = value
d2 now contains {('hello', 'how are you'): 1}
As you can see it contains only one value unlike in your example because dicts cannot contain the same key twice.
You won't get the expected output here, as both keys are now same. So, only one of them can be found in the resulting dict.
In [142]: d = {('hello', 'hi', 'how are you'): 2, ('hi', 'hello', 'how are you'): 1}
In [143]: {tuple(y for y in x if y!='hi'):d[x] for x in d}
Out[143]: {('hello', 'how are you'): 1}
This should do it
answer = {}
for k,v in d.iteritems():
key = tuple(i for i in k if i!='hi')
answer[key] = v
d = answer
Not sure if this works for you, but this creates a new dictionary and will sum the values of any keys that end up colliding after removing 'hi' (assuming that is what you want to do - if not, disregard this answer :) ):
>>> from collections import defaultdict
>>> new_d = defaultdict(int)
>>> for k, v in d.iteritems():
... new_d[tuple(i for i in k if i != 'hi')] += v
...
>>> new_d
defaultdict(<type 'int'>, {('hello', 'how are you'): 3})
This does not correspond with your output, but as explained by everyone else, dictionaries can only have one key of a particular value, so this combines into one.
So I realized that
dict1.update(dict2)
replaces values of dict2 with dict1 if the key exists in both the dictionaries. Is there any way to add the values of dict2 to dict1 directly if the key is present instead of looping around the key,value pairs
You say you want to add the values, but not what type they are. If they are numeric, you may be able to use collections.Counter instead of dict
>>> from collections import Counter
>>> a = Counter({'a':1, 'b':2})
>>> b = Counter({'a':5.4, 'c':6})
>>> a + b
Counter({'a': 6.4, 'c': 6, 'b': 2})
What is the best way to remove an item from a dictionary by value, i.e. when the item's key is unknown? Here's a simple approach:
for key, item in some_dict.items():
if item is item_to_remove:
del some_dict[key]
Are there better ways? Is there anything wrong with mutating (deleting items) from the dictionary while iterating it?
The dict.pop(key[, default]) method allows you to remove items when you know the key. It returns the value at the key if it removes the item otherwise it returns what is passed as default. See the docs.'
Example:
>>> dic = {'a':1, 'b':2}
>>> dic
{'a': 1, 'b': 2}
>>> dic.pop('c', 0)
0
>>> dic.pop('a', 0)
1
>>> dic
{'b': 2}
Be aware that you're currently testing for object identity (is only returns True if both operands are represented by the same object in memory - this is not always the case with two object that compare equal with ==). If you are doing this on purpose, then you could rewrite your code as
some_dict = {key: value for key, value in some_dict.items()
if value is not value_to_remove}
But this may not do what you want:
>>> some_dict = {1: "Hello", 2: "Goodbye", 3: "You say yes", 4: "I say no"}
>>> value_to_remove = "You say yes"
>>> some_dict = {key: value for key, value in some_dict.items() if value is not value_to_remove}
>>> some_dict
{1: 'Hello', 2: 'Goodbye', 3: 'You say yes', 4: 'I say no'}
>>> some_dict = {key: value for key, value in some_dict.items() if value != value_to_remove}
>>> some_dict
{1: 'Hello', 2: 'Goodbye', 4: 'I say no'}
So you probably want != instead of is not.
a = {'name': 'your_name','class': 4}
if 'name' in a: del a['name']
A simple comparison between del and pop():
import timeit
code = """
results = {'A': 1, 'B': 2, 'C': 3}
del results['A']
del results['B']
"""
print timeit.timeit(code, number=100000)
code = """
results = {'A': 1, 'B': 2, 'C': 3}
results.pop('A')
results.pop('B')
"""
print timeit.timeit(code, number=100000)
result:
0.0329667857143
0.0451040902256
So, del is faster than pop().
I'd build a list of keys that need removing, then remove them. It's simple, efficient and avoids any problem about simultaneously iterating over and mutating the dict.
keys_to_remove = [key for key, value in some_dict.iteritems()
if value == value_to_remove]
for key in keys_to_remove:
del some_dict[key]
items() returns a list, and it is that list you are iterating, so mutating the dict in the loop doesn't matter here. If you were using iteritems() instead, mutating the dict in the loop would be problematic, and likewise for viewitems() in Python 2.7.
I can't think of a better way to remove items from a dict by value.
c is the new dictionary, and a is your original dictionary, {'z','w'}
are the keys you want to remove from a
c = {key:a[key] for key in a.keys() - {'z', 'w'}}
Also check: https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch01.html
y={'username':'admin','machine':['a','b','c']}
if 'c' in y['machine'] : del y['machine'][y['machine'].index('c')]
There is nothing wrong with deleting items from the dictionary while iterating, as you've proposed. Be careful about multiple threads using the same dictionary at the same time, which may result in a KeyError or other problems.
Of course, see the docs at http://docs.python.org/library/stdtypes.html#typesmapping
This is how I would do it.
for key in some_dict.keys():
if some_dict[key] == item_to_remove:
some_dict.pop(key)
break