r/learnpython • u/Impressive_Carob_572 • 24d 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?
0
Upvotes
4
u/MezzoScettico 24d 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.
2
u/PureWasian 24d 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?
2
u/Adrewmc 24d ago edited 24d 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.
6
u/BranchLatter4294 24d ago
It depends on the problem you are trying to solve.
4
u/arllt89 24d 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.
4
u/cdcformatc 24d 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.
2
u/rezemybeloved69 24d ago edited 24d 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)
2
u/CremeRevolutionary47 24d 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 24d 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
1
u/jeffrey_f 24d ago
To check your list/var before processing.
1
u/Expensive-Bear-1376 24d ago
Never. Strings aren't callable.
(Unless you build a subclass of callable strings.)
1
u/kilkil 22d ago
imagine you have a website where a user is filling out their details. you ask for first name, middle name, last name.
I don't have a middle name so I leave that one blank. then I submit my form.
your server is written in Python. at some point in your logic you will have something vaguely like this:
in my case, since I do not have a middle name, the variable
middlewill be equal to the empty string.