Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

315
Views
.index() generates AttributeError: 'dict_values' object has no attribute 'index'

I'm trying to rewrite this Python2 code to Python3 accepted syntax. The .index() methods generates the following error:

AttributeError: 'dict_values' object has no attribute 'index'

This is because .index() is no valid syntax in Python3. I've read that a list should be used to work around the problem, but I can't figure out how to do it. Anyone any idea how to work around the problem?

words1 = [self._word_to_id.keys()[self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1)]
indices = [i for i, w in enumerate(words1) if w in PUNCTUATIONS]
for i in indices:
    words1[i], words1[i-1] = words1[i-1], words1[i] 
words2 = [self._word_to_id.keys([self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1, len(data_x))]
all_words = words1 + [puncts[-1]] + words2  
content = ' '.join(all_words)  
min_step = len(puncts)
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You are calling self._word_to_id.values() which returns the class dict_values and not list. dict_values does not inherit from list and does not have the index method because of that.

You need to convert your dictionary values into a list to use the index function. Try this:

list(self._word_to_id.values()).index(data_x[index])
over 4 years ago · Santiago Trujillo Report

0

words1 = [list(self._word_to_id.keys())[list(self._word_to_id.values()).index(data_x[index])] for index in range(len(puncts) - 1)]

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!