Novice Question: two lists -> dictionary

frederic pinel fpinel at fedex.com
Thu Apr 15 23:48:49 EDT 1999


Here is a quick and dirty answer! Sorry...

ListA = ['10', '10', '20', '20', '20', '24']
ListB = ['23', '44', '11', '19', '57', '3']

ListT = []	# temporary list
Dict = {}

for i in range(len(ListA)):
	ListT = key_values(i, ListA, ListB)
	if (Dict.has_key(ListT[0]) == 0):
		Dict[ListT[0]] = ListT[1:]
		
print Dict	# print results, for test purposes. 


def key_values(i, l_key, l_val):
	" returns a list with the first item being the l_key[i] (ListA), 
	" and the values after for that key (ListB)
	resultat = []
	for j in range(len(l_key)):
		if (l_key[j] == l_key[i]):
			result[0] = l_key[i]
			result.append(l_val[j])
		
	return result

regards,
frederic

jwtozer at my-dejanews.com wrote:
> 
> How do I make the members of one list the key of a dictionary and the members
> of a second list the members of list values associated with with those keys?
> 
> Given:
> 
> ListA = ['10', '10', '20', '20', '20', '24']
> ListB = ['23', '44', '11', '19', '57', '3']
> 
> Desired Result:
> 
> Dict = {'10': ['23','44'],'20': ['11','19','57'], '24': ['3']}
> 
> Any help will be much appreciated.
> 
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own




More information about the Python-list mailing list