Linked Questions

3359 votes
44 answers
2.2m views

How do I pass a variable by reference?

I wrote this class for testing: class PassByReference: def __init__(self): self.variable = 'Original' self.change(self.variable) print(self.variable) def change(self, ...
David Sykes's user avatar
  • 50.1k
0 votes
3 answers
182 views

Lists as values in a dictionary Python [duplicate]

ddic = {'a': 10,'b': 23,'c': [0, 1, 2]} n2 = ddic['c'] n2[-2] = 1000. ddic {'a': 10, 'b': 23, 'c': [0, 1000.0, 2]} Why changing the list at which n2 is pointing, changes also the list of dict ddic, ...
glopezluzzatti's user avatar
0 votes
1 answer
95 views

Why is list of Dataframes not being assigned in Python? [duplicate]

I attached an empty list of Dataframes into a variable. I used it in a for loop along with a list of columns so that I can transpose the columns into the index. Then reassigned it back into the ...
Marc_Law's user avatar
0 votes
0 answers
57 views

Why different reference is created for int object when passed to function in python? [duplicate]

As per my knowledge the Python interpreter creates references from 0 to 256 for int objects, at the start itself. Here I got output 3,[44,2,3], for the list I understood, but why did the same thing ...
user avatar
6 votes
1 answer
16k views

Is list pass by value or by reference? [duplicate]

Consider the following code, at first glance it does the same thing, but the result is different, sometimes it seems list is pass by value, sometimes list seems pass by reference: lst = [1, 2] def f(...
an offer can't refuse's user avatar
5 votes
4 answers
6k views

Rotation of the numbers-python

To find the rotations of a number I wrote a code like def rotation(N): A=[] for i in range(len(N)): y=N.pop(0) N.append(y) A.append(N) return A K=[1,9,7] r=...
Layla's user avatar
  • 127
10 votes
2 answers
228 views

References to mutables (e.g., lists) as values in Python dictionaries - what is best practice?

It is possible to map a dictionary key to a value that is a reference to a mutable object, such as a list. Such a list object can be changed by invoking a list method on the reference, and the ...
Tom Baker's user avatar
  • 693
0 votes
4 answers
109 views

Python Data Structure: Difference between two types of for-loop in python?

I was thinking this question should be asked on SO, but I was not able to find it somehow(Let me know in the comment section if there was one, i will delete this post) It has came to my attention ...
ZpfSysn's user avatar
  • 889
1 vote
2 answers
359 views

My minimax algorithm for Tic Tac Toe in Python is showing maximum recursion error

I am trying to write the code for minimax algorithm for tic tac toe in python all by myself, I have wrote the code but whenever the function is getting called it is showing a "maximum recursion depth ...
Golden Clips's user avatar
-1 votes
1 answer
149 views

Are Python's dictionary view objects an exception to its assignment by value nature

As per this question on Stack Overflow, assignments in Python are always by value, hence you cannot change the original source. For example (from the same question), locs = [ [1], [2] ] for loc in ...
InsaneCoder's user avatar
  • 8,378
3 votes
2 answers
70 views

Function in Python Setting Global Variables (without intent)

I don't know if this is a problem that others get, but I have code in python that goes like this: def makemove(board,move,val): new=board new[move[0]][move[1]]=val return new My problem ...
Nathan Eaves's user avatar
0 votes
1 answer
124 views

Why does assignment of pd.DataFrame.sort_values() not work inside loop?

import numpy as np import pandas as pd test_data = pd.DataFrame( dict( value=np.random.rand(9) - 0.5, group=np.repeat(np.arange(3), 3), time_idx=np.tile(np.arange(3), 3), ...
FabianH's user avatar