Hey all. I've decided I let my Python skills (minor though they were)
slip away so I started reading the new edition of Learning Python to
brush up. I just read about lists again and I'm wondering if someone
could explain what's going on under the hood that makes index and slice
assignments behave differently when assigning an empty list.
For example:
>>L = [1, 2, 3, 4, 5] L[0:2] = [] L
[3, 4, 5]
>>L = [1, 2, 3, 4, 5] L[0] = [] L
[[], 2, 3, 4, 5]
So the question is, when you assign an empty list to an index, why does
it insert an empty list, but when you assign an empty list to a slice,
it simply deletes the slice?
Thanks! 8 1514
John Salerno wrote:
So the question is, when you assign an empty list to an index, why does
it insert an empty list, but when you assign an empty list to a slice,
it simply deletes the slice?
I would say this is consistent behavior because a list slice is also a
list itself. Whereas a list element is just that. A reference to an
object that can be rebound. In the latter case you are rebinding a
single list item to an empty list.
John Salerno wrote:
Hey all. I've decided I let my Python skills (minor though they were)
slip away so I started reading the new edition of Learning Python to
brush up. I just read about lists again and I'm wondering if someone
could explain what's going on under the hood that makes index and slice
assignments behave differently when assigning an empty list.
For example:
>>L = [1, 2, 3, 4, 5]
>>L[0:2] = []
>>L
[3, 4, 5]
>>L = [1, 2, 3, 4, 5]
>>L[0] = []
>>L
[[], 2, 3, 4, 5]
So the question is, when you assign an empty list to an index, why does
it insert an empty list, but when you assign an empty list to a slice,
it simply deletes the slice?
Assignment to a list *element* rebinds the single element to the
assigned value. Assignment to a list *slice* has to be of a list, and it
replaces the elements in the slice by assigned elements.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Steve Holden <st***@holdenweb.comwrote:
Assignment to a list *element* rebinds the single element to the
assigned value. Assignment to a list *slice* has to be of a list, and it
replaces the elements in the slice by assigned elements.
Assignment to a list *slice* just has use an iterable, it doesn't actually
have to be another list.
Steve Holden wrote:
Assignment to a list *element* rebinds the single element to the
assigned value.
Ok, I understand that.
Assignment to a list *slice* has to be of a list [or iterable, as per
Duncan], and it
replaces the elements in the slice by assigned elements.
I don't understand the second part of that sentence. I'm assuming "it"
refers to the list being assigned, "replaces the elements" is
self-evident, but what does "by assigned elements" refer to? It seems
when you assign a list to a list slice, nothing gets replaced, the slice
just gets deleted.
John Salerno wrote:
Steve Holden wrote:
>Assignment to a list *element* rebinds the single element to the assigned value.
Ok, I understand that.
Assignment to a list *slice* has to be of a list [or iterable, as per
Duncan], and it
>replaces the elements in the slice by assigned elements.
I don't understand the second part of that sentence. I'm assuming "it"
refers to the list being assigned, "replaces the elements" is
self-evident, but what does "by assigned elements" refer to? It seems
when you assign a list to a list slice, nothing gets replaced, the slice
just gets deleted.
Deletion occurs *only* in the corner case where there are no "assigned
elements" i.e. only if the RHS list (sequence) is *empty*. Otherwise
there would be no point at all in the language having assignment to a
slice -- del L[0:2] would suffice.
Study these:
>>L = [1, 2, 3, 4, 5] L[0:2] = [] L
[3, 4, 5]
>>L = [1, 2, 3, 4, 5] L[0:2] = ['whatever'] L
['whatever', 3, 4, 5]
>>L = [1, 2, 3, 4, 5] L[0:2] = tuple('foobar') L
['f', 'o', 'o', 'b', 'a', 'r', 3, 4, 5]
>>>
John Salerno wrote:
>replaces the elements in the slice by assigned elements.
I don't understand the second part of that sentence. I'm assuming "it"
refers to the list being assigned, "replaces the elements" is
self-evident, but what does "by assigned elements" refer to? It seems
when you assign a list to a list slice, nothing gets replaced, the slice
just gets deleted.
>>x = range(5) x[0:3] = ["a", "b"] x
['a', 'b', 3, 4]
Here, '= ["a", "b"]' replaces x[0:3] with ["a", "b"]. When you do '=
[]', it replaces them with nothing.
--
John Machin wrote:
Deletion occurs *only* in the corner case where there are no "assigned
elements" i.e. only if the RHS list (sequence) is *empty*.
Oh, it was my understanding that deletion always occurs, even when the
section is being assigned a non-empty value, i.e. delete the slice and
insert new value.
Otherwise
there would be no point at all in the language having assignment to a
slice -- del L[0:2] would suffice.
Right, but I'm wondering why a statement like
L[0:2] = []
doesn't assign an empty list as the new element in L. For example:
L = [1, 2, 3, 4, 5]
L[0:2] = []
Why doesn't L now equal [[], 3, 4, 5] as it does with an index assignment?
>>L[0:2] = tuple('foobar')
>>L
['f', 'o', 'o', 'b', 'a', 'r', 3, 4, 5]
Hmm...why doesn't L equal [('f', 'o', 'o', 'b', 'a', 'r'), 3, 4, 5] ?
Shouldn't L be a 4 item list instead of 9?
"John Salerno" <jo******@gmailNOSPAM.comwrote in message
news:48***********************@cv.net...
| John Machin wrote:
|
| Deletion occurs *only* in the corner case where there are no "assigned
| elements" i.e. only if the RHS list (sequence) is *empty*.
|
| Oh, it was my understanding that deletion always occurs, even when the
| section is being assigned a non-empty value, i.e. delete the slice and
| insert new value.
Slice replacement means replace the slice with a new slice generated from
the iterable on the left. John meant that deletion only only happens when
the replacement is empty. Yes, deletion always occurs, but usually
addition also occurs, so the net result is replacement rather than just
deletion.
| Otherwise
| there would be no point at all in the language having assignment to a
| slice -- del L[0:2] would suffice.
|
| Right, but I'm wondering why a statement like
| L[0:2] = []
| doesn't assign an empty list as the new element in L. For example:
Because, as others already told you, slice replacement is slice
replacement, not item assignment. When you say to replace the slice with
nothing, the deleted slice is replaced with nothing.
L[0:2] = [[]]
says to replace the slice with a slice consisting of one item -- []
That will get you what you are expecting.
| L = [1, 2, 3, 4, 5]
| L[0:2] = []
|
| Why doesn't L now equal [[], 3, 4, 5] as it does with an index
assignment?
See above.
|
| >>L[0:2] = tuple('foobar')
L[0:2] = 'foobar' has same effect because s string is an iterable.
| >>L
| ['f', 'o', 'o', 'b', 'a', 'r', 3, 4, 5]
|
| Hmm...why doesn't L equal [('f', 'o', 'o', 'b', 'a', 'r'), 3, 4, 5] ?
| Shouldn't L be a 4 item list instead of 9?
Because you replaced 2 items with 6.
L[0:2] = ['foobar'] will replace 2 with 1, leaving 4
tjr This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Hilde Roth |
last post by:
This may have been asked before but I can't find it. If I have
a rectangular list of lists, say, l = ,,], is
there a handy syntax for retrieving the ith item of every sublist?
I know about for i...
|
by: jblazi |
last post by:
I should like to search certain characters in a string and when they are
found, I want to replace other characters in other strings that are at
the same position (for a very simply mastermind game)...
|
by: Uwe Mayer |
last post by:
Hi,
a class of mine should support the list interface and implements the __len__
and __getitem__ methods.
Now when I ask for an unbounded slice:
>>> len( myObj )
my __getitem__(self, y)...
|
by: kosh |
last post by:
I was wondering if there is or there could be some way to pass a generator an
optional starting index so that if it supported that slicing could be made
more efficient. Right now if you do use a...
|
by: Philippe C. Martin |
last post by:
Hi,
I'm looking for an easy algorithm - maybe Python can help:
I start with X lists which intial sort is based on list #1.
I want to reverse sort list #1 and have all other lists sorted...
|
by: xkenneth |
last post by:
Looking to do something similair. I'm working with alot of timestamps
and if they're within a couple seconds I need them to be indexed and
removed from a list.
Is there any possible way to index...
|
by: RMWChaos |
last post by:
Previously, I had used independent JSON lists in my code, where the lists were part of separate scripts. Because this method did not support reuse of a script without modification, I decided to...
|
by: jmDesktop |
last post by:
This program:
s = 'abcde'
i = -1
for i in range (-1, -len(s), -1):
print s, i
gives
abcd -1
|
by: Andreas Prilop |
last post by:
To get bold numbers in ordered lists, one can write
ol { font-weight: bold }
ol span { font-weight: normal }
<ol>
<li><span>......</span></li>
<li><span>......</span></li>
</ol>
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |