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 …
matchlist.append(i)
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
i=“c”
matchlist = [“a”,“b”]
matchlist.append(i)
print(matchlist)
[“a”, “b”, “c”] #output
i=str(i) , is also not required as the input you are providing is a string
you don’t need to assign matchlist to matchlist.append(i)
since matchlist is an array the element can be simply appended.
moreover you may avoid the line
i=str(i)
as it doesn’t bring any change to the code. since i is given the value of a string there is no point in writing this line.
you can write code for this problem:
List=[“a” , “b”]
print(List)
List.append(“c”)
print(List)
List.insert(1, “D”)
print(List)
in python after writing list , you need not assign the list name again just use the append() statement for adding the new element in list
here are the code:-
i=“c”
matchlist=[“a”,“b”]
matchlist.append(i)
print(matchlist)
In python programming language after writing list , You not need to assign the list name again just use the append() function of list, statement for adding the new element in list
after checking all condition that i have cleared with you then also you face same error then please check my code -
i=“c”
matchlist=[‘a’,‘b’]
matchlist.append(i)
print(matchlist)
Check My Code -
L1 = [“a”, “b”]
L1.append(“c”)
print(L1)
List=[“a” , “b”]
print(List)
List.append(“c”)
print(List)
List.insert(1, “D”)
print(List)
List=[“a” , “b”]
print(List)
List.append(“c”)
print(List)
List.insert(1, “D”)
print(List)
try this code… i feel that the error is with the quotations you are using… your quotes are behaving like a character other than “”
Correct code
i = “c”
matchlist = [“a”, “b”]
i = str(i)
matchlist.append(i)
print(matchlist)
line no3 is actually not required because you already set i as string c (anything inside a double or single quote is a string). you dont need to assign matchlist.append(i)
to matchlist.
CODE:
i = 'c'
matchlist = ['a','b']
matchlist.append(i)
print(matchlist)
i = “c”
matchlist = [“a”,“b”]
matchlist.append(i)
print(list)
for append function,instead of using matchlist = matchlist.append(i) ,use list.append(i).
In your code the third line(i=str(i)) is not required because you have already set i=‘c’ which represents a string in python.You don’t need to assign matchlist to matchlist.append(i).You can write your code as below:
i=“c”
matchlist=[“a”,“b”]
matchlist.append(i)
print(matchlist)
#Output:[“a”,“b”,“c”]
There should not be the append() method is assigned to the same array or list.
You can simply use " matchlist.append(i) " rather than assigning it to the matchlist variable.