This is something that has been in my mind since I knew about .sort()
and random.shuffle
.
although I couldn't find how .sort()
works but I went through the Lib\random.py
of course it was promising enough to be understood that how random.shuffle(list_name)
shuffles the list_name
itself but I found difficulty understanding how it worked there....
What I'm trying to understand:
>>> lst = [-5, 2, 0, -2, -4, -3, 3, 4, -1, 5, 1]
>>> lst.sort() #How this changes the value of list???
>>> lst
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
>>> random.shuffle(lst) #How does this function change the value of argument itself?
>>> lst
[-2, 2, -3, 1, 0, 5, -5, 4, 3, -1, -4]
A simple example will be helpful to understand:
>>> a = [1,3,5,7,9]
>>> a.square()
>>> a
[1,9,25,49,81]
>>> b = [-2,-1,0,3,4]
>>> cube(b)
>>> b
[-8,-1,0,27,64]
How can I make the above code happen?
def kyub(lst_):
return [i**3 for i in lst_]
here the above kyub()
returns a list what I am expecting is to return
nothing but change the lst_
itself.
To sum up
Assume we have a function y = f(x)
which means for some value of x=x0
there will be some y=f(x0)
which is similar to function = lambda x:f(x)
but I'm looking for a function g(x)
that returns nothing(None) but changes the value of x
itself.
Solution to the below problem can help me understand the logic behind this.
>>> x_list = [value1, v2, v3, v4]
>>> print(x_list)
[value1, v2, v3, v4]
>>> function(x_list)
>>> print(x_list)
[output_1, output_2, output_3, output_4] #Note: I printed the value of x_list which now has changed. it's not what I'd printed it before.