0

Please don't mark this as an answered question because I think it is not about the general program but here is a specific thing that I don't realize. So the problem is when I run the program it should first remove the "araba" string and then insert "car" string in its existing place. So I uptaded the dict everytime and print it but nothing happens? Thx

tr_mean=["araba","otobus","kus"]
en_mean=["car","bus","dog"]
dictionary={}
for x in tr_mean:
  index=tr_mean.index(x)
  dictionary[x]=en_mean[index]



print tr_mean.index("araba")
for x in tr_mean:
  index=tr_mean.index(x)
  dictionary[x]=en_mean[index]
print words
del tr_mean[tr_mean.index("araba")]
for x in tr_mean:
  index=tr_mean.index(x)
  dictionary[x]=en_mean[index]
print words
tr_mean.insert(0,"car")
for x in tr_mean:
  index=tr_mean.index(x)
  dictionary[x]=en_mean[index]

print words
user3483203
  • 50,081
  • 9
  • 65
  • 94
Elvez The Elf
  • 97
  • 1
  • 10

1 Answers1

1
tr_mean = ["araba", "otobus", "kus"]
en_mean = ["car", "bus", "dog"]
dictionary = {}
for x in tr_mean:
    index = tr_mean.index(x)
    dictionary[x] = en_mean[index]

print tr_mean.index("araba")

Here you are just doing the same thing again, not needed.

# for x in tr_mean:
#     index = tr_mean.index(x)
#     dictionary[x] = en_mean[index]

You did not define words, I guess you meant to use dictionnary?

print dictionary

Here you remove "araba" from tr_mean, so tr_mean is now ["otobus", "kus"]

del tr_mean[tr_mean.index("araba")]

After that, I'm guessing you try to "reset" the dictionnary

for x in tr_mean:
        index = tr_mean.index(x)
        dictionary[x] = en_mean[index]
    print dictionary

but all you are doing is:

dictionary["otobus"] = "bus"
dictionary["kus"] = "dog",

araba still exists in dictionnary!!!

Before you assign the new values, clear the dictionary with dictionary.clear()

dictionary.clear()
for x in tr_mean:
    index = tr_mean.index(x)
    dictionary[x] = en_mean[index]
print dictionary

Or better yet, remove that last block of code and simply delete "araba" from dictionary

del dictionary["araba"]

Every thing else is left unchanged.

tr_mean.insert(0, "car")
for x in tr_mean:
    index = tr_mean.index(x)
    dictionary[x] = en_mean[index]

print dictionary 

EDIT: If you want to add "car" as a new key to your dictionary using the value of "araba", you can do something like this:

dictionary["car"] = dictionary.pop("araba")

.pop() will delete the "araba" key and give the value associated with "araba" to the new key dictionary["car"]

Alexandre Cox
  • 510
  • 3
  • 9
  • Yeah thanks that seems to be the problem. I'm new to dictionaries and as I understood the problem is I deleted "araba" from tr_mean but the meaning "car" is not deleted from en_mean and I didn't delete the "araba" key from dictionary. So it didn't do anything to "araba" key. I think yes :D – Elvez The Elf May 03 '18 at 15:08
  • Found this post on dictionaries: https://stackoverflow.com/a/8381589/7612911. – Alexandre Cox May 03 '18 at 18:33