This question already has answers here:
Modifying a list inside a function
(4 answers)
How do I pass a variable by reference?
(39 answers)
Understanding Python's call-by-object style of passing function arguments [duplicate]
(3 answers)
Closed 4 years ago.
I am new to python. I searched a lot but couldn't find the reason why this is happening. Could anyone please tell me the difference between these two?
My general question is when using a list in function is by reference and when is by value?
My test function is acting as call by reference and test2 is acting as call by value.
I know in python everything is an object, but with that, I couldn't understand the difference.
tnx
def test(my_list):
for i in range(len(my_list)):
my_list[i] = 5
def test2(my_list1):
my_list1 = [6, 6, 6]
a = [4, 4, 4]
print(a)
test(a)
print(a)
test2(a)
print(a)
output:
[4, 4, 4]
[5, 5, 5]
[5, 5, 5]
Related
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
I know "how" to use the : operator to extract data from a list in python.
Eg:
l = [1,2,3]
print(l[0:1])
print(l[-1:])
Yields:
[1]
[3]
But, how does python literally interpret the :?
Why can't I set a variable equal to : and then call it?
Eg:
x = :
print(l[x])
Would yield:
[1, 2, 3]
Hoping someone who can go deeper on how python works can offer some insight.
Thanks!
: is shorthand for the slice class. You can assign instances of that class to variables for what you're trying to:
>>> l = [1, 2, 3]
>>> l[1:3]
[2, 3]
>>> l[slice(1, 3)]
[2, 3]
>>> x = slice(0, 2)
>>> l[x]
[1, 2]
This question already has answers here:
Finding max value in the second column of a nested list?
(5 answers)
Closed 2 years ago.
I'm catching up with Python3 so I hope I can get some slack :P
I have a list of lists, say:
results=[[1, 5000],
[2, 5000],
[3, 6666],
[4, 6250],
[5, 6000],
[6, 5833],
[7, 5714],
[8, 6250],
[9, 6111]]
And I would like to get the entry which has the biggest second value (in this case, the pair [3, 6666]).
Could I have some quick reference on how to get it?
Thanks!
Here is one approach using itemgetter()
import operator
>>> max(results, key=operator.itemgetter(1))
[3, 6666]
>>>
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.
In python 3.7.4 I wnat to sort the array but I can't. I used the function sort and wrote this code -
arr = [0,5,8,9,6,3,1,2]
print(arr.sort())
output
None
Please help me.
Python's list.sort() modifies the list in-place and does not return it. For example:
x = [0,5,8,9,6,3,1,2]
print(x) # [0, 5, 8, 9, 6, 3, 1, 2]
x.sort()
print(x) # [0, 1, 2, 3, 5, 6, 8, 9]
See the docs for more information on list methods.
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
How do I pass a variable by reference?
(39 answers)
Closed 4 years ago.
for some reason the variable "a" in the main function changes after the function call. In spite of the fact that "a" doesn't even change within the function. Although even if it did, such changes should not affect its value outside the function, right?
I am using Python 3.6, and I'm out of ideas as to why this is happening. Any help would be greatly appreciated
def mhmArgSort(myNumListInput):
myNumList=myNumListInput
cnt=0
endCnt=len(myNumList)
NumListSortedIndices=[]
for cnt1 in range(len(myNumList)):
smallestNum=100000000000
smallestNumIndex=0
for cnt in range(len(myNumList)):
if myNumList[cnt]<smallestNum:
smallestNum=myNumList[cnt]
smallestNumIndex=cnt
NumListSortedIndices.append(smallestNumIndex)
myNumList[smallestNumIndex]=100000000000
return NumListSortedIndices
a=[1,2,3,4,-1,0,3]
print(a)
b=mhmArgSort(a)
print(a)
print(b)
This produces the following result:
[1, 2, 3, 4, -1, 0, 3]
[100000000000, 100000000000, 100000000000, 100000000000, 100000000000, 100000000000, 100000000000]
[4, 5, 0, 1, 2, 6, 3]
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
So i'm looking for sort of a layman/beginner explanation as to what this means:
L[i:i+lensub]
What does that mean by it's self? by the the way lensub = len(sublist) and L is list.
What that code is doing is using Explain Python's slice notation to slice L from position i to i+lensub.
The format for slice notation is [start:stop:step].
Below is basic demonstration:
>>> lst = [1, 2, 3, 4, 5]
>>> # Get positions 1 to 3
>>> lst[1:3]
[2, 3]
>>>