Questions tagged [dictview]

Questions related to Python's dictionary views created with the keys, items and values methods (viewkeys, viewitems and viewvalues on Python 2.7)

13 questions
67
votes
3 answers

dict.keys()[0] on Python 3

I have this sentence: def Ciudad(prob): numero = random.random() ciudad = prob.keys()[0] for i in prob.keys(): if(numero > prob[i]): if(prob[i] > prob[ciudad]): ciudad = i else: …
Menticolcito
  • 769
  • 1
  • 5
  • 11
8
votes
2 answers

Python: Understanding dictionary view objects

I've been trying to understand built-in view objects return by .items(), .values(), .keys() in Python 3 or similarly by .viewitems(), .viewvalues(), .viewkeys(). There are other threads on that subject but none (even the doc) seems to described how…
scharette
  • 9,437
  • 8
  • 33
  • 67
4
votes
2 answers

Why exactly are .values() and .keys() considered O(1)?

Couldnt track down a solid enough reasoning for why dictionary functions such as .values() and .keys() are considered to be O(1) in big O notation. (not sure if .items() is also considered O(1) )
4
votes
1 answer

Why does dict unioned with dict.keys() return a set?

As I originally expected, the union of a dict and a set gives TypeError: >>> {1:2} | {3} Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for |: 'dict' and 'set' However, surprisingly,…
Andrew Sun
  • 4,101
  • 6
  • 36
  • 53
3
votes
1 answer

Do Python 2.7 views, for/in, and modification work well together?

The Python docs give warnings about trying to modify a dict while iterating over it. Does this apply to views? I understand that views are "live" in the sense that if you change the underlying dict, the view automatically reflects the change. I'm…
Ouroborus
  • 16,237
  • 4
  • 39
  • 62
2
votes
2 answers

python3: sum (union) of dictionaries with "+" operand raises exception

I'd like to avoid the update() method and I read that is possible to merge two dictionaries together into a third dictionary using the "+" operand, but what happens in my shell is this: >>> {'a':1, 'b':2}.items() + {'x':98, 'y':99}.items() Traceback…
etuardu
  • 5,066
  • 3
  • 46
  • 58
2
votes
1 answer

Why do set operators work with dict_key view objects but not the equivalent set methods?

Edit: Possible duplicate. Only after posting this question and looking in "related questions" was I able to find Why are set methods like .intersection() not supported on set-like objects?, this question may similar enough to be a duplicate. I am…
johnDanger
  • 1,990
  • 16
  • 22
2
votes
2 answers

Why are set methods like .intersection() not supported on set-like objects?

In Python 3.7, I'd like to calculate the intersection of two dictionaries' keys. To do this, I'd like to call the .intersection() method on their keys(), however it does not work. .keys() produces a set-like object, however most set methods don't…
hyperknot
  • 13,454
  • 24
  • 98
  • 153
2
votes
1 answer

Comparing lists with dictviews

Dictionary views "are set-like objects" and can thus be used to compare dictionary contents with other objects. Specifically, key-views: set-like value-views: not set-like item-views: set-like if (key, value) pairs are unique and hashable The…
pylang
  • 40,867
  • 14
  • 129
  • 121
2
votes
1 answer

dictionary lookup on Python 2.7 vs 3.4

This came up in a nitpick discussion about the prefered style for iterating over dictionary keys if you need to apply some test to the value. I was comparing the performance of [k for k in d if d[k] == 1] against [k for k, v in d.items() if v ==…
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
2
votes
4 answers

Inconsistent behaviour between dict.items and dict.values

Note: code examples in python3, but the question stands for python2 as well (replacing .keys with .viewkeys, etc) dict objects provide view methods which (sometimes) support set operations: >>> {'a': 0, 'b': 1}.keys() & {'a'} {'a'} >>> {'a': 0, 'b':…
wim
  • 338,267
  • 99
  • 616
  • 750
1
vote
3 answers

How to compare values of two dictionaries with list comprohension?

How to compare only the values of two dictonaries? So I have this: dict1 = {"appe": 3962.00, "waspeen": 3304.08} dic2 = {"appel": 3962.00, "waspeen": 3304.08} def compare_value_dict(dic): return dic def compare_value_dict2(dic2): …
mightycode Newton
  • 3,229
  • 3
  • 28
  • 54
1
vote
0 answers

dict_key and dict_value to list performances

With python 2.x we can do this: >>> d = {0:'foo', 1:'bar'} >>> d.keys() [0, 1] >>> d.keys()[0] 0 >>> d.values() ['foo', 'bar'] >>> d.values()[0] 'foo' With python 3.x, .keys() return dict_key and .values() return dict_value. I guess these view…
bux
  • 7,087
  • 11
  • 45
  • 86