def replace_query_param(url, key, val):
 (scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
 query_dict = urlparse.parse_qs(query, keep_blank_values=True)
 query_dict[key] = [val]
 query = urlparse.urlencode(sorted(list(query_dict.items())), doseq=True)
 return urlparse.urlunsplit((scheme, netloc, path, query, fragment))

def replace_query_param ( url , key , val ) : ( scheme , netloc , path , query , fragment ) = urlparse . urlsplit ( url ) query_dict = urlparse . parse_qs ( query , keep_blank_values = True ) query_dict [ key ] = [ val ] query = urlparse . urlencode ( sorted ( list ( query_dict . items ( ) ) ) , doseq = True ) return urlparse . urlunsplit ( ( scheme , netloc , path , query , fragment ) )
given a url and a key/val pair .