Unable to edit list after converting from tuple [duplicate] - python

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.

Related

list of dataframe append having issue [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 1 year ago.
I am trying to create a list of dataframes.
I print my dataframe normally and it works fine.
print(df)
however if I create a list to store my df, it does not seem to work coz it prints 'None' from listOfDataframe.
# Create list of data frame using list
listOfDataframe = []
listOfDataframe = listOfDataframe.append(df)
print(listOfDataframe)
whats happening and How do I fix it?
There is no need to reassign a list after appending something to it. It changes it automatically:
# Create list of data frame using list
listOfDataframe = []
listOfDataframe.append(df)
print(listOfDataframe)
It is returning None simply because the append method returns None.
See Henry Ecker's comments.

Issue in Python lists [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 3 years ago.
Here is my code:
list_b=[1,2,3,4,5,6,7,8,9,10]
print (list_b)
if(1 in list_b):
t=list_b.append('hello')
print(t)
else:
t1=list_b.append(100)
print(t1)
In the console it is showing me None. (Image)
append() method does not return any value but updates existing list.
if you want to see the updated list, use print(list_b).
print(t) or print(t1) will return None as they don't have any return values.
You haven't stated the issue, but I'm assuming it's this:
t=list_b.append('hello')
append() modifies the array, it doesn't return a new array.

Not able to return list [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 7 years ago.
Is there any difference between these two ways of returning lists?
Initially the list is empty.
my_list = []
method 1:
my_list.append(1)
return my_list
method 2
return my_list.append(1)
Actually, the second method is returning an empty list for me. Please clarify why it is happening like this
When you type the following:
return my_list
You are returning a list object. When you type the following:
return my_list.append(something)
You are returning the result of that method call. In the case of .append() that method is void, so you are effectively returning nothing. If the method .append() appended the argument you pass to it and then returned the modified list itself then you could do it, but that isn't the case.

Type error when trying to extend a list in Python [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 need to understand why :
years = range(2010,2016)
years.append(0)
is possible, returning :
[2010,2011,2012,2013,2014,2015,0]
and
years = range(2010,2016).append(0)
or
years = [0].extend(range(2010,2016))
doesn't work ?
I understand that it is a type error from the message I got. But I'd like to have a bit more explanations behind that.
You are storing the result of the list.append() or list.extend() method; both alter the list in place and return None. They do not return the list object again.
Do not store the None result; store the range() result, then extend or append. Alternatively, use concatenation:
years = range(2010, 2016) + [0]
years = [0] + range(2010, 2016)
Note that I'm assuming you are using Python 2 (your first example would not work otherwise). In Python 3 range() doesn't produce a list; you'd have to use the list() function to convert it to one:
years = list(range(2010, 2016)) + [0]
years = [0] + list(range(2010, 2016))
append and extend operations on lists do not return anything (return just None). That is why years is not the list you expected.
In Python 3 range returns an instance of the range class and not a list. If you want to manipulate the result of range you need a list, so:
years = list(range(2010,2016))
years.append(2016)
Finally, (and similarly to the append above) extend operates on the list you're calling it from rather than returning the new list so:
years = list(range(2010,2016))
years.extend( list(range(2016,2020)))
*While Python 2's range function does return a list, the above code will still work fine in Python 2*
Hope this helps!

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