How does python create an empty list?

This is my best guess.

I think that it might help to look at it like you’re making cuts in a piece of rope. Each split is a cut. So in the case of “mississippi”, “iss” makes two cuts. if you cut a piece of rope twice you will have three pieces. In the case of “apple” and “pp”, the rope will be cut once, so two pieces in this case.

How does Python generate an empty element in the case of “mississippi” and “iss”? You got me. but in makes sense in the rope analog.

Maybe someone with a better understanding of Python’s inner workings can shed some light on the subject.

Moderator Note: this post was originally in response to this post - which is now moved to the bottom of this thread for posterity


There is nothing between 'iss' and 'iss', which gets translated to ''. Everything between the separators is still a string.

1 Like

Do you know how python does this?

If I was a C++ programmer I would, so no. I understand what is happening, is all.

1 Like

No such thing as an empty element, what’s empty is the string, a lack of elements, not an empty one

there’s nothing special about it, if anything empty is the simplest case

like you pointed out, you will end up with one more piece than the amount of splits you made. splitting zero times results in one piece (also using zero, but also not special, there isn’t a at-least-once requirement, nor is 1 the smallest natural number)

what would be special is -1, there you’d run into some real problems.

Interesting. Do you know what is used as a place holder for an empty string in this case?

But isn’t it? 0 is not a natural number, but undefined. On the number line it is both Integer and Real.

That’s my point.
Nothing special needs to happen.

If you put blueberries in a basket, what is the first state of the basket?
Of course it’s a natural number.

What amount does a ruler start counting at?

What speed do you have when you begin moving?

Starting at 1 is skipping the first case because it’s so trivial and simple, and yet incredibly important and existing everywhere

An empty basked may have zero items in it, but that does not make zero a natural counting number. Natural numbers begin with 1.

Mind, it depends who you ask. One standard includes 0 and others do not. I’m stuck in the trenches of old-school thinking as taught in the sixties.

One supposes it depends on the basis of the number. For counting? or ordering? If cardinal, there is no zero; if ordinal there is a zero, as in zero-indexing.

I guess I don’t understand.

There’s nothing to understand. It’s not a special case. Nothing is different.

The problem lies in the assumption that it has to be different, which you are probably getting one way or another from the idea that counting needs to start at 1, which it doesn’t, because that’s the second case, that’s not how things start out, the first is at 0.
You’re asking how before showing that it is the case. It’s not, so the question of how doesn’t make sense.

When you copy a substring you’re going to have a start and a stop location. The difference is the length. A length of 0 is a start and stop at the same location, the same logic as when selecting substrings of other sizes apply, it is not a special case. This is the first natural length of a string. You’ll run into trouble if you want a length of -1, that would be special. You’re off by one in when things get weird.

How python accomplishes things is for most purposes of no concern. It is free to do it in any way, and to change how it does it whenever, whether during runtime or between versions or when it’s full moon.

What is of concern is what behaviour is promised to us.

This behaviour doesn’t come from how python happens to implement that function, it comes from what the function is fundamentally supposed to do.

I think I wasn’t clear about what I was asking. What I meant was what does python use as a placeholder for an empty string? Is there an invisible character or a zero-width space?

Let’s ask Python that question…

>>> str()

The expression is a new instance of the class. Given no input argument, it returns an empty string.

No need for a placeholder, an empty string is a perfectly valid value, there is no need for a special case here

If a string can be of length 1, 2, 3, and so on, then why not 0?

What I meant was that if there no such thing as an empty element, but there can be a an element with an empty string, what is the way python differentiates the two.

There has to be such a thing as both, else what are we differentiating?

>>> x = [,,,]
SyntaxError: invalid syntax

Proof there is no such thing as an empty element. Python won’t let the interpreter even compile the list above. There has to be an identifiable type before the comma.

Good point. So how does python denote an empty string? I mean other than the quotation marks the user sees. Does it have a value? Like none, undefined, True or false?

The only value an empty string can have is "" or ''. As we’ve seen already it is what the constructor returns. It’s not Python, as much as the str class definition for any string–a grouping of characters (or nothing) demarcated by quotes. There is no other associated value, and as @ionatan stated above, none is needed. It is nothing special.

I think that it’s a falsy value. There must be an associated Boolean value for testing, right?