0

Why would you create an empty list? Just wondering.

25th Aug 2016, 7:33 AM
Luke Skywalker
3 Answers
+ 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)
2nd Nov 2016, 2:54 PM
Daniel Mello
Daniel Mello - avatar
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).
17th Sep 2016, 8:38 AM
Jon Smith
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.
5th Nov 2016, 4:36 AM
Sultan Sikander
Sultan Sikander - avatar