This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed last month.
I am expecting it append 4 twice but it is displaying None.
b = [2,3]
b.append(4)
print(b.append(4))
append returns None
append adds the element but returns none
you can print b later to verify
b = [2,3]
b.append(4)
print(b.append(4))
print(b)
Related
This question already has answers here:
What is the difference between list and list[:] in python?
(7 answers)
Closed 3 years ago.
I have this below code
a = [1,2,3]
b = a
a is b #shows True
b = a[:]
a is b #shows False
a == b # shows True
I thought the value of [1,2,3] and a[:] would have the same id and they are the exact same object.
What exactly happens when a[:] is assigned to b?
I'm sorry if this question has already been asked before, could not find a perfect answer
a[:] is a shallow copy of a list (as a slice). It has the same numeric value, but it is not the original list. == Checks value, "is" is value and "identity" of the value.
This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 4 years ago.
image=[[1,1,1],[1,1,0],[1,0,1]]
visited = [[False] * len(image[0])] * len(image)
visited[0][0] = True
print((visited[1][0]))
Shouldn't the above python code print False? Why is the entire column being assigned as True?
The reference to [False] * len(image [0]) is repeated len(image) times, so modifying the first element of visited affects the second (and third).
This question already has answers here:
Understanding the "is" operator [duplicate]
(11 answers)
Two variables in Python have same id, but not lists or tuples
(5 answers)
Closed 5 years ago.
I understand that the "is" operator checks identity between two objects, but what is this?
a=25
b=25
a is b
True
Why is this true if
a = [1,2,3]
b = [1,2,3]
a is b
False
And from this post, https://www.quora.com/Python-is-operator, it says all immutable objects, like integers from the first example, will have the first example's result, True, but tuples are immutable and this happens:
a = (1,2)
b = (1,2)
a is b
False
Can someone explain please?
Thanks.
This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 6 years ago.
Im working on coding bat questions. trying to rotate the lift one to the left. My code always return None as a result. what can i do?
**
def rotate_left3(nums):
a = nums.pop(0)
return nums.append(a)
**
Because append is a method without a return value.
Any Python function without a return value will return None
Split the lines to return the appended list.
Or return nums + [a]
This question already has answers here:
Compute list difference [duplicate]
(17 answers)
Closed 7 years ago.
I have two lists and I want to print the difference between them (if there is 1 difference, it should print "1". How can I fix that?
So what I have is:
a= ["1","2","3"]
b= ["1","4","5"]
The answer should be 2.
It depends on what do you mean by difference. If they are equal in length and you want to find out the difference, do:
c = [i for i in a if i not in b]
print len(c)
Use set:
print len(set(L1) - set(L2))
Test:
>>> L1 = [1,2,5]
>>> L2 = [8,1]
>>> len(set(L1) - set(L2))
2