Error in coding Python

Hi there,

I’m trying to do something extremely simple. I have a list and want to append a variable to it (in the middle of a for loop).

i = “c”
matchlist = [“a”,“b”]
i = str(i)
matchlist = matchlist.append(i)

I expected to get a list ‘matchlist’ with two records (‘a’ and ‘b’) in it and then append a new record ‘c’ to that list. However instead the list loses its type and the value comes up as

Any pointers on where I’m going wrong?

Many thanks.

you dont have to do matchlist = matchlist.append(i)

matchlist is an array, so just append the element

reassigning matchlist = matchlist.append(i) will make matchlist as type of the append() operation.

to append you need not assign it to the list name again … you can simply right …

it will work…!

I have checked… you can try like this

In your code you don’t need to assign the method into the variable.As in python append is use as a method that will do changes to the list itself on which method is performed.So you just need to write

matchlist = [“a”,“b”]
[“a”, “b”, “c”] #output

i=str(i) , is also not required as the input you are providing is a string