0

I have a list, and I'm asking the user to insert some words into a list. I compare the words he entered and check if they exist in the first dictionary (if not, I remove that word).

Now, I want to have each words he inputed on the specific day. (for example, in the first iteration, id he enters test1 test2 I should have a dictionary like 0: test1 test2 and for the second iteration if he enters test0 to ONLY have 1: test0.

The problem is that at the second iteration I get everything: 1: test1 test2 test0. How can I fix this ?

import itertools

my_list1 = []
my_first_list = ["test", "test1", "test2"]

zile_muschi = {}
for i in range(0, 2):
    day = str(input("Day" + str(i) + "(spaces between words):"))
    my_list1.append(day)
    combined = list(itertools.chain(*map(str.split, my_list1)))

    for element in combined:
        if element not in my_first_list:
            print("Word: " + element + " is not correct!")
            combined.remove(element)
    zile_muschi[i] = combined
print(zile_muschi)
  • You should use `raw_input` instead of `input`. – toti08 Feb 11 '16 at 10:00
  • Then you are assigning the whole `combined` variable to each key of your `zile_muschi` dictionary, so each key will contain all the entries. – toti08 Feb 11 '16 at 10:01
  • @toti08 this appears to be Python 3 so [`input` is right.](http://stackoverflow.com/questions/954834/how-do-i-use-raw-input-in-python-3-1) – Stuart Feb 11 '16 at 10:30

2 Answers2

0

You should initialize your temporary list inside the cycle. With minimal changes your code will look like this:

import itertools

my_first_list = ["test", "test1", "test2"]

zile_muschi = {}
for i in range(0, 2):
    my_list1 = []
    day = str(input("Day" + str(i) + "(spaces between words):"))
    my_list1.append(day)
    combined = list(itertools.chain(*map(str.split, my_list1)))

    for element in combined:
        if element not in my_first_list:
            print("Word: " + element + " is not correct!")
            combined.remove(element)
    zile_muschi[i] = combined
print(zile_muschi)
Max
  • 315
  • 1
  • 13
0

How about to add a clear before the append, just like this:

my_list1.clear()

before the line:

my_list1.append(day)
melalonso
  • 228
  • 2
  • 9