About 88,900 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

    In Python 3.7, normal dictionaries are ordered, so you don't need to use OrderedDict anymore (but you still can – it's basically the same type). The CPython implementation of Python 3.6 already included that change, but since it's not part of …

  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. 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.

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

    Minor variation to user1850980's answer (for the question "How to initialize a list of empty dictionaries") using list constructor:

  6. 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:

  7. 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. –

  8. python - How to use a dot "." to access members of dictionary?

    Mar 1, 2010 · For example, compare the output of pprint.pprint(person) using this approach, versus what you would have gotten had you used a regular python dict. Admittedly, you could fix that difference by adding a __repr__ method to the my_dict class, but that means you are already no longer using this "Simplest solution" anymore.

  9. 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 ...

  10. python - Should I use a class or dictionary? - Stack Overflow

    I've updated @alexpinho98 answer for Python 3.12 because a lot has changed in the past 11 years. Results. Key Takeaways: Classes in Python 3 are a lot faster now. Use classes instead of dicts where performance is important. TypeDict has no affect on the performance of dicts. Plain dataclasses take twice as long to instantiate.

Refresh