About 81,400 results
Open links in new tab
  1. In Python, when to use a Dictionary, List or Set?

    Jul 1, 2019 · When you want to collect an immutable ordered list of elements, use a tuple. (For example, when you want a (name, phone_number) pair that you wish to use as an element in a set, you would need a tuple rather than a list since sets require elements be immutable). When you want to collect a mutable ordered list of elements, use a list. (For ...

  2. python - How to index into a dictionary? - Stack Overflow

    Dictionaries are unordered in Python versions up to and including Python 3.6. If you do not care about the order of the entries and want to access the keys or values by index anyway, you can create a list of keys for a dictionary d using keys = list(d), and then access keys in the list by index keys[i], and the associated values with d[keys[i]].

  3. How to use a Python dictionary? - Stack Overflow

    Jul 13, 2017 · Dictionaries are the python equivalent of Hash table. It stores values as key-value pairs. Values can be any python objects whereas, keys need to immutable. Below are some of the ways to iterate over a dictionary in python

  4. How can I create an array/list of dictionaries in python?

    First, you should never use dict as a variable name, and second, you're creating a tuple (not a list) that contains the same dictionary three times, so every change you make to one of them affects all the others.

  5. python - Intersecting two dictionaries - Stack Overflow

    Then, instead of dict1, use {v: k for k, v in dict1.items()} and instead of dict2 use {v: k for k, v in dict2.items()}. And, if you want the case where it's either the key or the dictionary, then use the union of the intersection given in the answer and the intersection given in this comment. –

  6. python - Using a dictionary to select function to execute - Stack …

    @Ben Believe me, I am very aware of that, I have 8 years providing software solutions, this is the first time I use Python and I wish I could use many security features from other languages, however I can not control it, I need to make the module somewhat "Read-Only", the costumer asks us to give them "proofs" that the scripting feature is as ...

  7. python - Adding dictionaries together - Stack Overflow

    Here are quite a few ways to add dictionaries. You can use Python3's dictionary unpacking feature: ndic = {**dic0, **dic1} Note that in the case of duplicates, values from later arguments are used. This is also the case for the other examples listed here.

  8. python - How can I use if/else in a dictionary comprehension?

    Mar 22, 2020 · You've already got it: A if test else B is a valid Python expression. The only problem with your dict comprehension as shown is that the place for an expression in a dict comprehension must have two expressions, separated by a colon:

  9. python - Slicing a dictionary - Stack Overflow

    Use a set to intersect on the dict.viewkeys() dictionary view: l = {1, 5} {key: d[key] for key in d.viewkeys() & l} This is Python 2 syntax, in Python 3 use d.keys(). This still uses a loop, but at least the dictionary comprehension is a lot more readable. Using set intersections is very efficient, even if d or l is large. Demo:

  10. python - Hashing a dictionary? - Stack Overflow

    Jul 26, 2019 · Some of the values in d could be dictionaries too, and their keys will still come out in an arbitrary order. As long as all the keys are strings, I prefer to use: json.dumps(d, sort_keys=True) That said, if the hashes need to be stable across different machines or Python versions, I'm not certain that this is bulletproof.