Python Forum
Dictionaries homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionaries homework
#1
Hello everyone,
So basically i am trying to solve an homework about dictionaries:
"We will call two words “opposite” if one is equal to
the other read backwards. For example, words “deer” and “reed” are opposite. The function
should accept as a parameter a list of words lst and return a list of all
pairs of opposite words from lst . For example, for a list:
[ "according", "deer", "net", "ten", "reed", "refer",
"raw", "war", "addition", "frequency", "platform" ]
your function should return: [('deer', 'reed'), ('net', 'ten'), ('raw', 'war')]
The result should:
• contain every pair of words only once – notice that there is ('deer', 'reed')
but no ('reed', 'deer')
• not contain palindromes (words which are opposite to themselves).
That's my code, but it doesn't work..
lst= [ "deer", "net", "malayalam", "ten", "reed"]

# def find_opposites(lst):

new_lst =[]	
tup = []

for word in lst : 

	rev_word = word[::-1]

	if rev_word in lst:

		tup.append(word)
		tup.append(rev_word)
		tup = tuple(tup)
		tup=[]
		new_lst.append(tup)
		lst.remove(word)
		lst.remove(rev_word)
		

		
	else: 

	 	break 



print(new_lst)
		
But is not working and i don't understand why..
Reply
#2
pythoncrazy1 Wrote:lst.remove(word)
It's usually not a good idea to remove items from a list while you're iterating on that list with a for loop.
pythoncrazy1 Wrote:break
Are you going to exit the loop the first time you meet a word which opposite is not in the list?
Reply
#3
I removed them so they do not appear again when i print, and for the second question , actually not, when the word which opposite is not in the list i need to remove that word from the final output.
Reply
#4
Look at line 17, I'm pretty sure whatever your issue is revolves around that line.

If not, please share your current output so we don't have to guess what the issue is.
Reply
#5
I would say line 17 is in the wrong place. You are changing tup from a list to a tuple. But if you do that, you can't append to it any more. So line 17 is meant (I think) to change it back to an empty list.

The thing is, this is way more work than you need to do to create a tuple. You can just create one with tup = (word, rev_word). In this code, there is no reason to even store it in a variable. You could just create it and append it at the same time (new_lst.append((word, rev_word))).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Hello, Thank you for the answers so basically my output is fine if in the list there are two-faced words (deer,reed) , but when in the list there is a word that is a palindrome such as "abba" then i get an error , here you can see my code:

lst= (["deer", "reed", "palindromo"])


def find_opposites(lst):

	new_lst =[]	
	tup = []

	

	for word in lst : 
		
		# tup = []
		rev_word = word[::-1]

		if rev_word in lst:
			
			tup.append(word)
			tup.append(rev_word)
			# tup = tuple(tup)
			
			lst.remove(word)
			lst.remove(rev_word)
			new_lst.append((word,rev_word))

		# if word != rev_word :
			
		# 	new_lst.append()
			# 

			
		# else: 

		#  	break 



	return new_lst

print(find_opposites(lst))
Output:
[('deer', 'reed')]
lst= (["deer", "reed", "palindromo", "abba"])


def find_opposites(lst):

	new_lst =[]	
	tup = []

	

	for word in lst : 
		
		# tup = []
		rev_word = word[::-1]

		if rev_word in lst:
			
			tup.append(word)
			tup.append(rev_word)
			# tup = tuple(tup)
			
			lst.remove(word)
			lst.remove(rev_word)
			new_lst.append((word,rev_word))

		# if word != rev_word :
			
		# 	new_lst.append()
			# 

			
		# else: 

		#  	break 



	return new_lst

print(find_opposites(lst))
Output:
Traceback (most recent call last): File "C:\Users\user\Desktop\opposites.py", line 41, in <module> print(find_opposites(lst)) File "C:\Users\user\Desktop\opposites.py", line 24, in find_opposites lst.remove(rev_word) ValueError: list.remove(x): x not in list
Reply
#7
As was mentioned before, you shouldn't be removing items from a list while you're iterating over the list. It'll cause strange errors, and doesn't even make sense in this case (you have one list, and you're building a new one... why change the old list?).

I think you should just check to make sure the rev_word is different from word.
Reply
#8
As nilamo and Gribouillis said, you shouldn't modify the list you are looping through. It's also bad to delete from a list that was passed to a function. This will change the list outside the function, which may not be expected. To avoid having the same pair in the list twice, you should check before appending (word, rev_word) to new_lst to make sure that (rev_word, word) isn't already in new_lst.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Thank for your help, the code now is working properly :)
Reply


Forum Jump:

User Panel Messages

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