Python - Extending a list directly results in None, why? [duplicate] - python

This question already has answers here:
How do I concatenate two lists in Python?
(31 answers)
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 5 months ago.
x=[1,2,3]
x.extend('a')
Output:
x is [1,2,3,'a']
But when I do the following:
[1,2,3].extend('a')
Output:
None
Why does extend work on a list reference, but not on a list?
2nd Part:
I found this because I was trying to append a listB to a listA while trying to extend listC to listB.
listA.append([listB[15:18].extend(listC[3:12])])
Supposing lists cannot be directly appended / extending. What is the most popular work around form for resolving this issue?

list.extend modifies the list in place and returns nothing, thus resulting in None. In the second case, it's a temporary list that is being extended which disappears immediately after that line, while in the first case it can be referenced via x.
to append a listB to a listA while trying to extend listC to listB.
Instead of using extend, you might want to try this:
listA.append(listB[15:18] + listC[3:12])
Or do it in multiple simple lines with extend if you want to actually modify listB or listC.

extend will extend list it self. Return type of that method is None
If you want to union 2 list and add that list to another list then you have to use another way to add.
listB[15:18] = listC[3:12]
listA.extend(listB)

Related

In colab, reverse a list then return a NoneType [duplicate]

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 5 months ago.
I have a list M, containing 3 numpy arrays.
when I wrote M = M.reverse(), then M became the noneType.
Why this happens? To me the result should be a reversed list.
Thanks
Lists belong to the collection data types in Python and are implemented as objects with a set of collection methods, where not all methods return a value.
Specifically .reverse() is a method that directly modifies the list, but does not return a value, therefore to access the reversed list, you just use the variable it is assigned to (M in your case).

Unable to edit list after converting from tuple [duplicate]

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 4 years ago.
I'm using the code below:
a = ('Monty Python', 'British', 1969) #a is a tuple
b=list(a) #this should convert it to a list if I'm not wrong
print(b) #the output till here is okay
c=b.append("abcd")
print(c) # the output for this is None
Can anyone explain why am I unable to edit after converting the tuple to a list??
.append() does not return a list.
You are doing c = b.append("abcd"), this makes no sense because b.append() does not return a list, it returns none.
Try print(type(b.append("abcd"))) and see what it prints. So as you can see python is working correctly.
Things like .append() .pop() do not return a new list, they change the list in memory.
This is called an inplace operation I believe
You're printing c whose job is to append. Print b instead, that's your list.

Lists getting deleted in Python [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 3 years ago.
I am using Python 3.6
I have a list:
listA = [1,2,3,1,2,4]
I am trying to remove the repetitive items from the list, so the final list will be
listA = [3,4]
After I loop once and remove 1s from the list using pop, my loop automatically advances to 3, instead of 2.
To avoid this, I used following logic:
ListB= ListA
ListA.clear()
ListA = ListB
but once I clear ListA, the other list ListB is also getting cleared automatically. How can I avoid this or solve this issue?
Objects in Python are stored by reference,which means you didn't assign the value of ListA to ListB, but a pointer to the object.You can use is operator to test if two objects have the same address in memory.
Sequences can be copied by slicing so you can use this to copy a list:
b = a[:]
Also you can use
b = list(a)
Or you can use copy() module:
from copy import copy
b = copy(a)
See more details from How do I copy an object in Python?
Names of lists in python are actually just references so your new list is a reference to the same list. To actually make a new list you can use
ListB = list(ListA)

python a=[[],]*10 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python list confusion
When I write a simple python script, I define a new list as follows:
A=[[],]*10
What I wanna do is just initialize a list whose length is 10 and each element is a empty list.
And when I do something like:
A[0].append(["abc",])
I just wanna append this ["abc",] to A[0], and it turn out to be that every elements in A is appended with ["abc",]
I know this probably due to my initialization (A=[[],]*10) . I just want to know why this happened.
Your expression A=[[]]*10 is equivalent to:
a = []
A = [a]*10
This means that A consists of 10 references to one and the same list.
You are creating a list with 10 references to the same empty list.
You should use a list comprehension instead:
A = [[] for _ in range(10)]
In a list comprehension, you create a new list for every iteration through the loop. If you are using python 2, you should use xrange instead of range (although with only 10 elements that won't make much difference).
All the empty lists are references to the same object.

Python Sort() method [duplicate]

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 4 months ago.
I am starting to learn Python.
Can someone explain why sort() returns None?
alist.sort() ## correct
alist = blist.sort() ## NO incorrect, sort() returns None
Why shouldn't
alist = blist.sort()
return the sorted list and give it back to alist? This does not make sense to me.
Thanks.
alist.sort() sorts alist in-place, modifying alist itself.
If you want a new list to assign somewhere, use blist = sorted(alist)
list.sort(): http://docs.python.org/library/stdtypes.html#mutable-sequence-types
sorted(): http://docs.python.org/library/functions.html#sorted
Use the following:
alist = sorted(blist)
When you want to perform the sorting on same list then you have to use sort() method of list. But If you dont want to change the sequence of original list but you need a sorted copy of the original list then use sorted() inbuilt function.

Categories