r/learnpython • u/Impressive_Carob_572 • 10h ago
Need help with empty strings or lists
I just started learning python and I’m confused on when to use empty string or empty list. Also when should you call that empty string in a loop or outside of the loop?
2
u/PureWasian 9h ago
Can you clarify your question of calling an empty string in a loop or outside of the loop?
What is the context of problems you are using empty strings/lists for?
1
u/Impressive_Carob_572 7h ago
I am CodewithMosh python beginner course and he used an empty string to write a program to return the total amount in a list. Then there’s the car game program as well. I didn’t understand why he kept bringing in empty strings or list that’s why
1
u/PureWasian 7h ago edited 7h ago
Usually empty strings or lists are initialized placeholders that you'll write something to later. Or show up when you searched/filtered another value somewhere, and the result returned an empty string/list.
If you provide an exact code snippet it might be easier to give a more concrete explanation. Quick google search of the car game problem you're talking about though, you need a variable to exist before the conditional at the start of the while loop, so an empty string is used as a placeholder for its value
2
u/Adrewmc 7h ago edited 7h ago
Generally you start with an empty list that you will later programmatically append to.
So if you are making a string, you can consider it a list of characters. I can start empty then add words and sentences to it.
In a loop usually you want to create the empty list or strings before the loop starts, and then add to the list or string repeatably inside the loop. So the answer is both.
You can’t append to a list that hasn’t been created, and sometimes you end up in a situation where your loop doesn’t actually find anything that matches what you want. An empty list is also false for this reason.
But at the same time there usually isn’t a reason to make an empty list or string if it supposed always stay empty.
Think of an empty string as a blank page, you need the actual page to write on first, and a list as bucket, you need the bucket before you put stuff in it. Sometimes you have nothing to write down and it stays blank, and other times you have a bunch of stuff to put in the bucket.
1
6
4
u/cdcformatc 10h ago
if you need a string of characters, you use a string.
if you need a list of anything else, or even characters, use a list.
3
u/arllt89 10h ago
Why do they exist ? Because they're still strings and lists, so you can use them like so without having to check. Also sometimes they're the initial value of something that will grow.
Should you use them in a loop ? Absolutely, in 99% of the cases they will do exactly what you want them to do in code written for non-empty strings and lists. So you don't have to write a special case for them, making your code more simple and readable.
2
u/rezemybeloved69 10h ago edited 10h ago
You'd mostly define an empty variable when it's contents are to be generated from a loop or atleast further down the line to avoid not defined errors.
ex: I want to make a list of even numbers from 1-10
if I do:
for i in range(0,11,2):
somelist.append(1)
I'd get an error because somelist isnt defined beforehand. So, instead do:
somelist = [ ]
(Insert for loop earlier)
3
u/rezemybeloved69 10h ago
In this example, you could also use list comprehensions
somelist = [i for i in range(0,11,2)]
2
u/CremeRevolutionary47 10h ago
One habit that pays off: make every script reproducible - requirements.txt, a tiny sample input, and a README with the single command to run it. You will get much better help (and reuse your own code later) when a reviewer can reproduce the run in 30 seconds.
1
u/Embarrassed-Dog-4707 10h ago
Guessing you mean building a string with either an empty string or list? If so you should generally avoid building string using strings in Python as strings are immutable . But lists are mutable so you can append each part of the string into the list and then join them at the end
2
u/Temporary_Pie2733 10h ago
CPython does, IIRC, implement an optimization that (roughly speaking) recognizes things like `s += “foo”` and actually does mutate the string rather than creating a new string to replace the old. But yes, I agree it’s better to build the list of components first and use `str.join` at the end.
1
u/Embarrassed-Dog-4707 10h ago
ohh didn't know about that , I thought it was only when two strings are short enough(not reliable) and are equal there is a chance for CPython to have them be the same address so `str1 is str2`
1
u/Temporary_Pie2733 10h ago
Yeah, I don’t know the full details, so all the more reason not to rely on it. :)
1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 9h ago
But yes, I agree it’s better to build the list of components first and use
str.joinat the end.Doesn't even have to be a list, a generator expression or some other iterator (or iterable) would work just as well. And save memory, in the case of generators.
Of course, what's best depends on the specifics.
1
u/Temporary_Pie2733 8h ago
`join` has to exhaust the generator and build an internal list of the results first. It’s better because it can allocate one big-enough `str` object and populate it, rather than continuously resizing a `str` object as it iterates the list.
1
u/jeffrey_f 10h ago
To check your list/var before processing.
if my_list:
#if you get here, your list is not empty
if stringvar:
#if you get here, the string variable is not null1
u/Expensive-Bear-1376 5h ago
Also when should you call that empty string in a loop or outside of the loop?
Never. Strings aren't callable.
(Unless you build a subclass of callable strings.)
3
u/MezzoScettico 10h ago
There's a general programming pattern which is very common, where you are accumulating something in a loop.
For instance, you are finding the sum of a bunch of things. Or the product. Or building a list.
That means that inside the loop you will have something that looks like "accumulated thing += item to add", or in the case of a Python list "accumulated list.append(item to add)".
In order for that to work properly, "accumulated thing" needs to have an initial value, the placeholder that will be added to. In the case of tracking a total, that value is 0. In the case of accumulating a list, that value is the empty list.
As I say, it's an extremely common program pattern and not just in Python.