About 143,000 results
Open links in new tab
  1. Creating a new dictionary in Python - Stack Overflow

    Feb 4, 2020 · It clearly states that you're creating a dictionary, whereas the use of {} is ambiguous (same construct would be used to create an empty set). – Jeff Wright Commented Apr 24, 2024 at 20:44

  2. dictionary - How to initialize a dict with keys from a list and empty ...

    Python how to use defaultdict fromkeys to generate a dictionary with predefined keys and empty lists 4 How do I initialize a dictionary with a list of keys and values as empty sets in python3.6?

  3. python - How can I add new keys to a dictionary? - Stack Overflow

    You can use the dictionary constructor and implicit expansion to reconstruct a dictionary. Moreover, interestingly, this method can be used to control the positional order during dictionary construction (post Python 3.6). In fact, insertion order is guaranteed for Python 3.7 and above!

  4. Python - Trying to create a dictionary through a for loop

    Apr 19, 2017 · This is a more complicated version of the problem but it is still essentially creating a dictionary with a for loop. I wanted to create a database of all my photos/videos on my computer - 1000s of them and create a dictionary record for each photo which used the name of the photo/movie to identify the record.

  5. python - How to copy a dictionary and only edit the copy - Stack …

    Mar 18, 2010 · To create a copy of nested or complex dictionary: Use the built-in copy module, which provides a generic shallow and deep copy operations. This module is present in both Python 2.7 and 3.*

  6. python - How to create a dictionary of two pandas DataFrame …

    FWIW, the dictionary comprehension is redundant here. You can just use dict(df.values) instead. However, the dict comp does let you reverse k and v easily if needed, like {k: v for v,k in df.values} .

  7. python - Using a dictionary to count the items in a list - Stack …

    Sep 12, 2019 · create a for loop that will count for the occurrences of the "key", for each occurrence we add 1. for element in elements: if element in counts: counts[element] +=1 check whether we encountered the key before or not if so we add 1, if not we use "else" so the new key is added to the dictionary. >>> else: >>> counts[element] = 1

  8. python - Create a dictionary with comprehension - Stack Overflow

    Apr 9, 2022 · This syntax was introduced in Python 3 and backported as far as Python 2.7, so you should be able to use it regardless of which version of Python you have installed. A canonical example is taking two lists and creating a dictionary where the item at each position in the first list becomes a key and the item at the corresponding position in the ...

  9. python - Creating a dictionary from an iterable - Stack Overflow

    Oct 15, 2013 · You may want to consider using the defaultdict subclass from the standard library's collections module. By using it you may not even need to iterate though the iterable since keys associated with the specified default value will be created whenever you first access them.

  10. python - Converting dictionary to JSON - Stack Overflow

    May 28, 2019 · json.dumps() is used to decode JSON data json.loads take a string as input and returns a dictionary as output. ...

Refresh