How to create an empty list of fixed size in python?

+1 vote
asked Aug 6, 2016 by Hitesh Garg (352 points)  

I want to create a list of that can hold certain amount of elements say 10 and later assign values to them.
How should I do this whether to iterate over each element and assign values or is there any better and more pythonic way of writing it?

1 Answer

+1 vote
answered Aug 28, 2016 by Hitesh Garg (352 points)  
edited Apr 15, 2018 by Hitesh Garg
Best answer

Yes there are more pythonic way to do this job. Use any of the method as per your need.

To Creating an empty list:

>>> l = [None] * 10
>>> l
[None, None, None, None, None, None, None, None, None, None]

You can also use range(x) function to create a list of form [0, 1, 2, ... x-1]

>>> l = range(10) # for python 2.x and for python 3.x use list(range(x))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Using a function to create a list:

>>> def createList():
...     mylist = []
...     for i in range(9): 
...         mylist.append(i)
...     return mylist
>>> print(createList())
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Programming Tip : Did you know now all your essential programming tools are just a single click away when you use remotely accessible cloud hosted citrix xendesktop from CloudDesktopOnline at an affordable xendesktop pricing. If you prefer a dedicated server, Try a dedicated gpu server hosting from Apps4Rent.

124 questions

41 answers


928 users

Most popular tags

java python python3 oracle database java-8 sql-server python2 oracle11g git oracle-12g command-line maven sql-server-2012 sql input array list fenitoina dilantin javaçš„ angular javascripts git-branch intellij-idea hashmap collection file-io io temovate clobetasol ampicillin principen daclatasvir daklinza vasotec enalapril nexium esomeprazole tadalafil tadlis sildenafil citrate pefomax xeloda capecitabina clobetasol temovate stemetil prochlorperazine methotrexate methotrexate sildenafilo y dapoxetina super kamagra norvasc amlodipine citalopram celexa amitriptylin saroten vitamin b12 methylcobalamin valaciclovir valtrex zudena udenafil sildenafil citrate vigour depakote divalproex retin-a tretinoine sildenafil citrate ip-max nizoral ketoconazol ciaton tadalafil norethisteronacetat aygestin indomethacin indocin tiova tiotropium bromid finasterida proscar lithium carbonate eskalith finasterida propecia levitra vardenafil vantin cefpodoxima loxitane loxapine rulide roxithromycine avodart dutasterid tecnomax sildenafil citrate methylcobalamin vitamin b12 permethrin acticin etinilestradiol norgestrel ovral pk-merz amantadin cycrin medroxyprogesterone serophene clomiphene spironolactone aldactone mesalazin pentasa zebeta bisoprolol cytodrox hydroxyurea ampicillin ampicillin pletal cilostazol paracetamol panadol pepcid famotidina meclizine hydrochloride antivert donaton tadalafil sdf dsfs sdgfgsdg mac null-pointer-exception constructor oracle-12c terminal hibernate-5 hibernate git-merge es6 programming git-amend git-commit java-stream git-remote multithreading access-modifiers nested-classes
Welcome to Codingeek Q&A, where you can ask questions and receive answers from other members of the community.