def multidict(ordered_pairs):
 d = defaultdict(list)
 for (k, v) in ordered_pairs:
  d[k].append(v)
 for (k, v) in d.items():
  if (len(v) == 1):
   d[k] = v[0]
 return dict(d)

def multidict ( ordered_pairs ) : d = defaultdict ( list ) for ( k , v ) in ordered_pairs : d [ k ] . append ( v ) for ( k , v ) in d . items ( ) : if ( len ( v ) == 1 ) : d [ k ] = v [ 0 ] return dict ( d )
convert duplicate keys values to lists .