Python Forum
A strange value in dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A strange value in dictionary
#1
Hello,
I'm trying to write a solution for exercise 12.4 from "Think Python", and I have a strange behavior of my return value.
Here's the code:
def tuple_list_dict_init(w_list):
	"""
	Return a dictionary that have as keys tuples of letters(sorted) from words in a given list.
	w_list:	a given list of words. Should be of type list of strings.
	d = dict()
	"""
	for l in w_list:					#	this loop for test purposes only
		if (type(l) is int):
			print('Integer detected')
	
	for w in w_list:
		if (type(tuple(sorted(w))) is int):		#	this conditional for test purposes only
			print('Integer key detected')
		d.setdefault(tuple(sorted(w)), [])
		
	return d
When I call this function no 'print' is executed. But in the returned dictionary I can find key of type 'int'.
Where is the error? Thank you.
Reply


Messages In This Thread
A strange value in dictionary - by python_user_n - Jan-07-2019, 01:21 PM
RE: A strange value in dictionary - by buran - Jan-07-2019, 01:23 PM
RE: A strange value in dictionary - by buran - Jan-07-2019, 01:35 PM
RE: A strange value in dictionary - by perfringo - Jan-07-2019, 01:40 PM
RE: A strange value in dictionary - by buran - Jan-07-2019, 01:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Nested dictionary acting strange Pedroski55 2 2,223 May-13-2021, 10:37 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020