Function don't update the list realtime python [duplicate] - python

This question already has answers here:
Appending to a list comprehension in Python returns None
(3 answers)
Closed 2 years ago.
def func(a,b):
a=a.append(b)
if a is not None:
for i in a:
print(i)
li=[1,2,3]
def testcall():
c=10
func(li,c)
if __name__=='__main__':
test()
Why it is not printing the updated list
even though is I update the list in test function and then sent it, it'll not print anything. Does anyone have any clue why the function is having this strange behavior.
If I let the "func" to only accept the one argument and sent update the list in "test" It'll still don't show anything.

To answer your main concern of "why doesn't it print anything", when you perform a = a.append(b), the inner a.append(b) returns nothing, so a is nothing. Also, a major issue exists with your code because it lacks correct indentation and you've misnamed your call to testcall() as test(). This is probably what you want.
def func(a,b):
print(a) # [1,2,3]
a.append(b)
print(a) # [1,2,3,10]
if a is not None:
for i in a:
print(i)
li=[1,2,3]
def testcall():
c=10
func(li,c)
if __name__=='__main__':
testcall()
Some experiments you can do from within IDLE:
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = a.append(b)
>>> c
>>> a
[1, 2, 3, [4, 5, 6]]
>>>

Related

Exhaustion of iterator by zip in Python [duplicate]

This question already has answers here:
Zipped Python generators with 2nd one being shorter: how to retrieve element that is silently consumed
(8 answers)
Closed 1 year ago.
just out of curiosity I wanted to ask, why is the following wanted behavior in Python?
Suppose we have:
a = [1,2,3]
b = [1,2,3]
c = [1,2,3,4]
ai = iter(a)
bi = iter(b)
ci = iter(c)
Now, I expected the following example to print [4], however, it prints [].
for _ in zip(ci, zip(ai, bi)):
pass
print(list(ci))
Console:
>>> print(list(ci))
[]
But when I change arguments of the outter zip, it prints what I expect ([4]).
for _ in zip(zip(ai, bi), ci):
pass
print(list(ci))
Console:
>>> print(list(ci))
[4]
Shouldn't both example print the same result? Thanks.
next() of a zip iterator will try to read the next element of each of its argument iterators in order. If any of them reports that it's exhausted, the zip iterator will stop and report that it's exhausted.
This means that it will perform an extra iteration on all the initial arguments that are longer than the shortest one. This means that it reads an extra element of c.
You can see this ordering and extra iteration in the equivalent code in the documentation.
Change c to [1, 2, 3, 4, 5] and you'll get [5] at the end.

Why hasn't this list changed? [duplicate]

This question already has answers here:
Python functions call by reference [duplicate]
(12 answers)
Closed 3 years ago.
In the following code, the function modify_list want to modify b, but it failed, the print result is [1,2,3]. why hasn't the list a changed?
def modify_list(b):
b = [1,2]
a = [1,2,3]
modify_list(a)
print(a)
If you want something like this to work you can do:
def modify_list(b):
return [1,2]
a = [1,2,3]
a = modify_list(a)
print(a)
Or
def modify_list(b):
b[:] = [1,2]
a = [1,2,3]
modify_list(a)
print(a)
you are declaring other local variable b, if you want to mutate you can do:
b[:] = [1, 2]
or even better you can return your desired value for list a
if you want to change your list a value you can assign the desired value:
a = [1, 2]

Why isn't the value of the list changing even after I modify its value inside the function? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 3 years ago.
We know that lists in Python are mutable objects. However, the following code does not change the value of the list whose value is being modified inside a function.
def change(l):
l=l[2:5]
return()
l=[1,3,4,5,2,10]
change(l)
print (l)
I expected an output [4,5,2] but it's showing the results [1,3,4,5,2,10].
Exactly as UnhloySheep and Mark Meyer states, changing the code from
l = l[2:5]
to
l[:] = l[2:5]
should fix your issue.
You complete code should be:
def change(l):
l[:]=l[2:5]
return()
l=[1,3,4,5,2,10]
change(l)
print (l)
this print as your answer.
[4, 5, 2]
As you mutate the list inside a function, you will need to return the updated list like:
def change(l):
l=l[2:5]
return l
You aren't mutating the list; you are just changing the value of the local variable l. Instead, modify the object referenced by that variable.
def change(lst):
del lst[5:] # or lst[5:] = []
del lst[:2] # or lst[:2] = []
# or just l[:] = l[2:5], which I forgot about until reading César Correa's answer
some_list = [1,3,4,5,2,10]
change(some_list)
print (some_list)
def change(l):
l[:] = l[2:5]
The above code will work.
Normal slicing changes the reference of the list after slicing. You can check this by using
id() before and after slicing

Variable name as function argument? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 6 years ago.
I wish to make a function whose arguments are the name of a list L, and its arguments. The list L has just numbers in it, and I want to round them all to 1 decimal(by the way, the elements of the list L should be replaced by the rounded numbers, I don't want a new list M with them). Sadly, the list name varies in the script I'm planning, and so does the length of such lists. Here is my failed attempt:
def rounding(name,*args):
M=[round(i,1) for i in args] #here is the list I want L to become
name=M #here I try to replace the previous list with the new one
morada=[1,2.2342,4.32423,6.1231] #an easy example
rounding(morada,*morada)
print morada
Output:
[1, 2.2342, 4.32423, 6.1231] #No changes
Have the rounding function return a value.
def rounding(list_):
return [round(i, 1) for i in list_]
Then you can do this:
>>> morada=[1,2.2342,4.32423,6.1231] #an easy example
>>> morada = rounding(morada)
>>> morada
[1, 2.2, 4.3, 6.1]
Or if you really really wanted it to assign within the function you could do this:
def rounding(list_):
list_[:] = [round(i,1) for i in args]
Close. Lists are mutable, so...
name[:] = M
You can use eval()
For example, the following will start with a list containing [1, 2, 3, 4] and change the first element to 5:
list_0 = [1, 2, 3, 4]
def modify_list(arg):
list_1 = eval(arg)
list_1[0] = 5
modify_list('list_0')
print list_0

list.append() in python returning null [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 6 years ago.
What is the actual difference between list1.append() and list1+list2 in python??
Along with this, why the following statement return NULL?
print(list1.append(list2))
{where list1 and list2 are 2 simple list}
list.append() modifies the object and returns None.
[] + [] creates and "returns" new list.
https://docs.python.org/2.7/tutorial/datastructures.html#more-on-lists
Recommended reading: http://docs.python-guide.org/en/latest/writing/gotchas/
Returning None is a way to communicate that an operation is side-effecting -- that is, that it's changing one of its operands, as opposed to leaving them unchanged and returning a new value.
list1.append(list2)
...changes list1, making it a member of this category.
Compare the following two chunks of code:
# in this case, it's obvious that list1 was changed
list1.append(list2)
print list1
...and:
# in this case, you as a reader don't know if list1 was changed,
# unless you already know the semantics of list.append.
print list1.append(list2)
Forbidding the latter (by making it useless) thus enhances the readability of the language.
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a.append(b) # append just appends the variable to the next index and returns None
>>> print a
[1,2,3,[4,5,6]]
>>> a.extend(b) # Extend takes a list as input and extends the main list
[1,2,3,4,5,6]
>>> a+b # + is exactly same as extend
[1,2,3,4,5,6]
When you print a function, you print what it returns and the append method does not return anything. However your list now has a new element. You can print the list to see the changes made.
list1 + list2 means that you combine 2 lists into one big list.
list1.append(element) adds one element to the end of the list.
Here's an example of append vs +
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a + b
[1, 2, 3, 4, 5, 6]
>>>
>>> a.append(b)
>>> a
[1, 2, 3, [4, 5, 6]]

Categories