-1

Coming from a MATLAB background and looking to get some clarification on the following behavior:

When looping on a list of lists, the looped variable seems to be affected within the loop:

foo = [['string1'],['string2'],['string3']]

for item in foo:
    item.append('test')

print(foo)


[['string1', 'test'], ['string2', 'test'], ['string3', 'test']]

Why does foo update when item is being appended?

On a similar note, why do some methods need to be assigned an output and other don't? For example on lists, you can append by simply:

list.append(new_item)

But for strings, the output needs to be assigned:

string = string.replace('old','new')

GolferDude
  • 36
  • 3
  • What is it you think this code *should* be doing, and why? – Scott Hunter Jan 15 '21 at 18:53
  • 1
    Different methods do different things; some change their argument(s), some return a value, some do both. – Scott Hunter Jan 15 '21 at 18:54
  • because `item` refers to an object that is inside your list. You modify that object, so obviously that object will be modified in the list (because it is *the same object*). If you did `item = foo[0]; item.append('test'); print(foo[0])` what would you expect? – juanpa.arrivillaga Jan 15 '21 at 18:54
  • See https://stackoverflow.com/a/8056598/271415 – jarmod Jan 15 '21 at 18:56
  • Since you are new to python, you should probably read: https://nedbatchelder.com/text/names.html – juanpa.arrivillaga Jan 15 '21 at 18:58
  • @ScottHunter @juanpa.arrivillaga. I see what the code is doing but I was expecting was for `item` to be a separate instance of `foo[0]`. In MATLAB, when reassigning an array or referencing a subset of an array, the new variable is independent and any changes on the new variable don't affect the original array. I'm guessing this isn't the case in Python? If you assign a subset of a list and change it, it'll change the original list? – GolferDude Jan 15 '21 at 19:15
  • 1
    @GolferDude again read: https://nedbatchelder.com/text/names.html Python is a very different language than matlab. Python lists are not like matlab arrays, for something like that, use a `numpy.array`. In any case, Python **never** implicitly creates copies. You are merely referencing objects with new names – juanpa.arrivillaga Jan 15 '21 at 19:21

2 Answers2

0

You can 'Retype' using x = int/float/str(134324) here u have example:

x_int = 13
print(type(x_int))`
y_str = ('d')
print(type(y_str))
print('and here we can change type')
x_str = str(13)
print(type(x_str))
y_int = int('d')
#You can retype just something. 
#you can retype int into float
#you can retype int and float into string
#you CANT retype string into float or int
  • 1
    I don't understand how this addresses the question...and "retype" is not really standard terminology, you don't generally *change* the type of an object, you create a *new object* of a different type – juanpa.arrivillaga Jan 15 '21 at 19:12
  • But even using that terminology, "you CANT retype string into float or int" sure you can. `int("10")`, or `float("1.23")` – juanpa.arrivillaga Jan 15 '21 at 19:12
  • im talking about words; I didnt find any english terminology for 'Přetypování', its something, what change type. – Martin Trnka Jan 15 '21 at 19:14
  • "type conversion" probably. but in any case, i still don't see what this has to do with the OP's question – juanpa.arrivillaga Jan 15 '21 at 19:19
0

I found the answer in regards to copies and references in Python. More info on this thread:

python: changes to my copy variable affect the original variable

And this reference from @juanpa.arrivill:

https://nedbatchelder.com/text/names.html

Thanks/

GolferDude
  • 36
  • 3