Python Forum
Help With While Loop and Append
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help With While Loop and Append
#11
Ok so numbers, strings, and tuples are immutables, but lists are mutable.

There was this example in link:
nums = [1, 2, 3]
other = nums
nums.append(4)
print(other) ## 1, 2, 3, 4

is there any way where I can transfer only values of nums to other. like only values of one list of numbers to the values of second list of numbers without linkining them. cause I'll be using the main list repeatedly?
Reply
#12
(Feb-06-2019, 05:55 AM)laprus Wrote: is there any way where I can transfer only values of nums to other. like only values of one list of numbers to the values of second list of numbers without linkining them. cause I'll be using the main list repeatedly?

In [1]: nums = [1, 2, 3]

In [2]: other = nums[:]        # slice

In [3]: id(nums)
Out[3]: 4384668552

In [4]: id(other)
Out[4]: 4383868296

In [5]: nums.append(4)

In [6]: nums
Out[6]: [1, 2, 3, 4]

In [7]: other
Out[7]: [1, 2, 3]
You should keep in mind that it is only one level deep. Meaning that if list elements are mutable then slice is mutating as well:

In [8]: nums = [[1], [2], [3]]

In [9]: other = nums[:]

In [10]: nums[-1].append(4)

In [11]: nums
Out[11]: [[1], [2], [3, 4]]

In [12]: other
Out[12]: [[1], [2], [3, 4]]
There is of course built-in module copy for deep copies.

For complete picture one additional example of expected behaviour:

In [13]: del nums[-1]       

In [14]: nums
Out[14]: [[1], [2]]

In [15]: other
Out[15]: [[1], [2], [3, 4]]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#13
Thanks. I tried your code in python and that works just as I needed. I guess I was looking for ':' operator.

I didn't understand why you wrote 'In' and 'Out' at every alternate line. I had to delete the code in python for it to work.
Reply
#14
(Feb-07-2019, 04:58 AM)laprus Wrote: I didn't understand why you wrote 'In' and 'Out' at every alternate line. I had to delete the code in python for it to work.

Sorry about that. This is not 'me', it's ipython who does it on my behalf Smile .

I use interactive interpreter in terminal for one-liners/short code examples and if I copy from there it is how ipython displays it. If you are interested you can have a look at Project Jupyter which provides notebook functionality on top of ipython.

I don't expect oneliners to be copied. I strongly recommend that you enter them yourself. There are several reasons for that: getting comfortable with writing code, muscle memory, you have possibility to understand/learn keywords, functions, methods etc. Nowadays, to be 'computer literate' you must be able to type at least 200 strokes per minute so for one-liners it is much faster to type instead of copying.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  why is the append function gets empty on the loop?/python rhai 3 3,148 Jul-07-2018, 12:19 PM
Last Post: rhai

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020