Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dynamically create python dictionary

dict= {} # create an empty dictionary
list= [['a', 1], ['b', 2], ['a', 3], ['c', 4]]
#list is our input where 'a','b','c', are keys and 1,2,3,4 are values
for i in range(len(list)):
     if list[i][0] in dic.keys():# if key is present in the list, just append the value
         dic[list[i][0]].append(list[i][1])
     else:
         dic[list[i][0]]= [] # else create a empty list as value for the key
         dic[list[i][0]].append(list[i][1]) # now append the value for that key
Comment

dynamically create python dictionary

dict= {} # create an empty dictionary
list= [['a', 1], ['b', 2], ['a', 3], ['c', 4]]
#list is our input where 'a','b','c', are keys and 1,2,3,4 are values
for i in range(len(list)):
     if list[i][0] in dic.keys():# if key is present in the list, just append the value
         dic[list[i][0]].append(list[i][1])
     else:
         dic[list[i][0]]= [] # else create a empty list as value for the key
         dic[list[i][0]].append(list[i][1]) # now append the value for that key
Comment

PREVIOUS NEXT
Code Example
Python :: variable globale python 
Python :: python with braces 
Python :: how to get a specific character in a string on number python 
Python :: blender change text during animation 
Python :: astype float across columns pandas 
Python :: NumPy flipud Example 
Python :: python autoclick website 
Python :: python check if string contains symbols 
Python :: pyqt math 
Python :: mean absolute error in machine learning formula 
Python :: unicode error python 
Python :: python dict items 
Python :: how to get cpu model in python 
Python :: sanke in python 
Python :: division in python 
Python :: pyqt graph 
Python :: python sleep command 
Python :: how to sum all the values in a list in python 
Python :: python pyttsx3 
Python :: check if boolean is true python 
Python :: python check empty string 
Python :: Function to plot as many bars as you wish 
Python :: sum of even numbers 
Python :: palindrome of a number in python 
Python :: python list comprehensions 
Python :: python write float with 2 decimals 
Python :: heroku how to access config vars django 
Python :: pathy python 
Python :: python find length of list 
Python :: google youtuve api 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =