Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python defaultdict to dict

>>> #You can simply call dict:
>>> a
defaultdict(<type 'list'>, {'1': ['b', 'a'], '3': ['b'], '2': ['a']})
>>> dict(a)
{'1': ['b', 'a'], '3': ['b'], '2': ['a']}

# but remember that a defaultdict is a dict
# (with some special behavior, check source):
>>> isinstance(a, dict)
True
Comment

defaultdict python dict inside dict

nums = [1, 2, 1, 1, 4, -4, 2, 2, 4, 2, 5, 7]
dict_in_dict = defaultdict(dict)
for i in range(0, len(nums), 3):
    dict_in_dict[nums[i+1]][nums[i]] = -1*nums[i+2]
dict(dict_in_dict)

# result
# {2: {1: -1, 2: -4}, 4: {1: 4}, 5: {2: -7}}
Comment

PREVIOUS NEXT
Code Example
Python :: downgrade python version windows 
Python :: django background_task 
Python :: pass 2d array to 1d python 
Python :: string equals python 
Python :: python return to top of loop 
Python :: python tableau 2d 
Python :: array creation in numpy 
Python :: argparse flag without value 
Python :: reply_photo bot telegram python 
Python :: read xml file in python 
Python :: moving element to last position in a list python 
Python :: How to Connect Google Colab to a Local Jupyter Runtime 
Python :: codechef solution 
Python :: Python program to count all characters in a sentence 
Python :: multiple inputs in one line- python 
Python :: python outlook 
Python :: google sheet api python 
Python :: binary to decimal python 
Python :: create Pandas Data Frame in Python 
Python :: most common letter in string python 
Python :: python3 format leading 0 
Python :: combining strings 
Python :: Python get first element from list 
Python :: groupby fillna 
Python :: get type name python 
Python :: filter query objects by date range in Django 
Python :: Subset data frame by date 
Python :: destructuring for dict in python 
Python :: python data type conversion 
Python :: python gitignore 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =