How to add new value to a list without using 'append()' and then store the value in a newly created list?
Question:
I have been trying this a lot.
>>> x = [4,5]
>>> y = x.append(7)
>>> print y
None
>>>print x
[4, 5, 7]
How is this possible?
When I trying storing the values in the new list y
and the print it, it results in None
and it also changes the current list `x.
Is there any other way to do this in Python?
Answers:
You can do
x = [4,5]
y = x + [7]
# x = [4, 5]
# y = [4, 5, 7]
This is possible because x.append()
is a method of list x
that mutates the list in-place. There is no need for a return value as all the method needs to do is perform a side effect. Therefore, it returns None
, which you assign your variable y
.
I think you want to either create a copy of x
and append to that:
y = x[:]
y.append(7)
or assign y
the result of a list operation that actually creates a new list:
y = x + [7]
Because the function append()
modifies the list and returns None
.
One of the best practices to do what you want to do is by using +
operator.
Let’s take your example :
>>> x = [4, 5]
>>> y = x + [7]
>>> x
[4, 5]
>>> y
[4, 5, 7]
The +
operator creates a new list and leaves the original list unchanged.
The y
is None
because the append()
method doesn’t return the modified list (which from your code you are expecting).
So you can either split the statement,
y = x.append(7)
into
x.append(7)
y = x
OR use,
y = x + [7]
The second statement is more cleaner and creates a new list from x
.
Just a note: Updating y
won’t make any updates in x
as in the first statement it does. If you want to avoid this use copy.copy
Using Extend,
x = [3,4]
y =[]
y.extend(x+[7])
print(x, y)
produces output :
[3, 4] [3, 4, 7]
[Program finished]
#this program help you in inserting multiple
y=[]
for i in range (3):
x=[input("list value =")]
y=y+x
I have been trying this a lot.
>>> x = [4,5]
>>> y = x.append(7)
>>> print y
None
>>>print x
[4, 5, 7]
How is this possible?
When I trying storing the values in the new list y
and the print it, it results in None
and it also changes the current list `x.
Is there any other way to do this in Python?
You can do
x = [4,5]
y = x + [7]
# x = [4, 5]
# y = [4, 5, 7]
This is possible because x.append()
is a method of list x
that mutates the list in-place. There is no need for a return value as all the method needs to do is perform a side effect. Therefore, it returns None
, which you assign your variable y
.
I think you want to either create a copy of x
and append to that:
y = x[:]
y.append(7)
or assign y
the result of a list operation that actually creates a new list:
y = x + [7]
Because the function append()
modifies the list and returns None
.
One of the best practices to do what you want to do is by using +
operator.
Let’s take your example :
>>> x = [4, 5]
>>> y = x + [7]
>>> x
[4, 5]
>>> y
[4, 5, 7]
The +
operator creates a new list and leaves the original list unchanged.
The y
is None
because the append()
method doesn’t return the modified list (which from your code you are expecting).
So you can either split the statement,
y = x.append(7)
into
x.append(7)
y = x
OR use,
y = x + [7]
The second statement is more cleaner and creates a new list from x
.
Just a note: Updating y
won’t make any updates in x
as in the first statement it does. If you want to avoid this use copy.copy
Using Extend,
x = [3,4]
y =[]
y.extend(x+[7])
print(x, y)
produces output :
[3, 4] [3, 4, 7]
[Program finished]
#this program help you in inserting multiple
y=[]
for i in range (3):
x=[input("list value =")]
y=y+x