About 60,100 results
Open links in new tab
  1. python - Create a dictionary with comprehension - Stack Overflow

    Create a dictionary with list comprehension in Python I like the Python list comprehension syntax. Can it be used to create dictionaries too? For example, by iterating over pairs of keys and …

  2. Python Dictionary Comprehension - Stack Overflow

    710 There are dictionary comprehensions in Python 2.7+, but they don't work quite the way you're trying. Like a list comprehension, they create a new dictionary; you can't use them to add keys …

  3. How can I use if/else in a dictionary comprehension?

    Feb 25, 2012 · 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 …

  4. python - Filter dict to contain only certain keys? - Stack Overflow

    Aug 6, 2010 · Uses dictionary comprehension. If you use a version which lacks them (ie Python 2.6 and earlier), make it dict((key, old_dict[key]) for ...). It's the same, though uglier. Note that …

  5. python - Iterate over a dictionary by comprehension and get a ...

    Mar 7, 2014 · Iterate over a dictionary by comprehension and get a dictionary [duplicate] Asked 11 years, 6 months ago Modified 11 years, 6 months ago Viewed 35k times

  6. Conditional expressions in Python Dictionary comprehensions

    Aug 15, 2013 · Conditional expressions in Python Dictionary comprehensions Asked 12 years, 2 months ago Modified 7 years, 2 months ago Viewed 51k times

  7. Add an element in each dictionary of a list (list comprehension)

    Dec 28, 2012 · You are using a list comprehension incorrectly, the call to returns a value and thus your newly created list will be full of values instead of your expected values. You need only to …

  8. Nested dictionary comprehension python - Stack Overflow

    Jul 29, 2013 · I haven't found an example of a nested dictionary comprehension like this; Googling "nested dictionary comprehension python" shows legacy examples, non-nested …

  9. python - Make a dictionary (dict) from separate lists of keys and ...

    Imagine that you have: keys = ('name', 'age', 'food') values = ('Monty', 42, 'spam') What is the simplest way to produce the following dictionary ? dict = {'name' : 'Monty', 'age' : 42, 'food' : …

  10. dictionary - Is a python dict comprehension always "last wins" if …

    Sep 24, 2016 · If I create a python dictionary with a dict comprehension, but there are duplicate keys, am I guaranteed that the last item will be the one that ends up in the final dictionary? It's …