0
Why would you create an empty list? Just wondering.
3 Respuestas
+ 1
"""you can add the elements anytime you want as in a loop iteraction. this gives to you construct lists dynamically. there are other methods to manipulate lists. hope to help you."""
mylist=[]
print('before:')
print(mylist)
i=1
while i <= 4:
mylist.append(i)
i+=1
print('after:')
print(mylist)
0
I'm not sure about Python but many languages won't permit to add an item to a list which hasn't been created, so you need to first create an empty list, so that you may (or may not) add items to it.
One scenario would be if you are need populate list by adding values one by one using a loop (e.g. asking the user to enter values). You need to create the list before you start the loop, but you have no data yet, so you create an empty list. Also you no idea how many items will be added to the list. it may be 1 or 1,000,000 or it may be 0. At some later point, you will want to iterate through that list (using a loop), an empty list won't be a problem - however, a trying to iterate through a nonexistent list will give an error (at least in most languages, I haven't tested it in Python).
0
you can create an empty list just for make its scope global, mean if you create a list start of code and then can assign value to.list in Any function or code block and can also access any where in code file.