I am using .append to fill in an empty list with a string but my item is being appended multiple times. After the first string is appended 5 times and the second string is appended 4 times. Why is this happening?
Here is the code I am using
kw_list_1 = []
def add_kw():
kw_1 = raw_input('enter your first keyword - ')
for kw in kw_1:
kw_list_1.append(kw_1)
kw_2 = raw_input('enter second keyword - ')
for kw2 in kw_2:
kw_list_1.append(kw_2)
print kw_list_1
For example, if I use 'apple' for kw_1 and 'pear' for kw_2 this is returned
['apple', 'apple', 'apple', 'apple', 'apple', 'pear', 'pear', 'pear', 'pear']
What’s going on here? Is append not the correct way to approach this?
it’s due to the
forloop.just use
kw_list_1.append(kw_1)remove the for loop.