In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.
Code Like a Pythonista: Idiomatic Python
11–20 of 59 posts
Re: Code Like a Pythonista: Idiomatic Python
#12In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.
x = [0] * 100
Re: Code Like a Pythonista: Idiomatic Python
#13In the Java ecosystem, they call it best practices. But of course using the phrase "best practices" these days is considered taboo. Thus the use of the word "idiomatic"?
Re: Code Like a Pythonista: Idiomatic Python
#14In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.
First thing I would do is ask why I need that; I've never come across a case where I needed a pre-built list of items all initialized to a common or empty value - if you're doing that, I would explore some sort of custom generative process (building the items of your list as you need them). But, just because I can't think of a good use case for this (where I couldn't use generative recursion or iteration) doesn't mean you haven't. Weigh what you are doing.
I would do this one of two ways: create a function that will produce an object that will generatively build the list (like xrange, but pass an "initializer" value, like myxrange(0, 100, initialize=0)). See the source for Python's xrange to do this it should be quite easy.
Or do this in a while loop (flat, easy to understand, and you aren't being too naughty by producing more than once list):
ls = []
cnt = 0
while cnt
Alternatively you can do the above with a for loop but you would have to iterate over a sequence (which means creating a list to do it); the while loop is going to be the most efficacious way of doing what you want I think, excluding building a custom xrange style implementation.Re: Code Like a Pythonista: Idiomatic Python
#15In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.
x = [0] * 100
Is it efficient though?
Re: Code Like a Pythonista: Idiomatic Python
#16In the Java ecosystem, they call it best practices. But of course using the phrase "best practices" these days is considered taboo. Thus the use of the word "idiomatic"?
An idiom is rather a preferred practice.
If you take some time to read through some Python source, you'll notice that, although the code uses the exact same constructs that you are used to in Java, it does so in a manner you might find unfamiliar or unpredictable.
One example is the use of try/except instead of if/else (beg for forgiveness rather than ask permission). Another is Duck-typing rather than Interface (if it quacks like a duck...)
Coming from Java and looking at some Python code without forewarning or context, you might just be tempted to label the whole thing as really really bad practice, when in truth, it's just Idiomatic Python.
Re: Code Like a Pythonista: Idiomatic Python
#17In case there are Pythonistas here who will indulge a question: one thing I've never figured out how to do elegantly ("idiomatically", I suppose) is create a list of predetermined size initialized with a constant(usually zeros). I always end up with something kludgey like: x = [0 for i in xrange(0..100)] which seems so roundabout it can't possibly be the right solution.
Yeah, that is kludgy (you're creating two lists now!). First thing I would do is ask why I need that; I've never come across a case where I needed a pre-built list of items all initialized to a common or empty value - if you're doing that, I would explore some sort of custom generative process (building the items of your list as you need them). But, just because I can't think of a good use case for this (where I coul…
If you just need an iterable, itertools.repeat(0, 100) will do the trick.
Re: Code Like a Pythonista: Idiomatic Python
#18Earlier quoted context omitted.
x = [0] * 100
This is much better than my own comment - I never even knew about that syntax! Is it efficient though?
>>> [0] + [0] + [0]
[0, 0, 0]
>>> [0] * 3
[0, 0, 0]Re: Code Like a Pythonista: Idiomatic Python
#19Earlier quoted context omitted.
Yeah, that is kludgy (you're creating two lists now!). First thing I would do is ask why I need that; I've never come across a case where I needed a pre-built list of items all initialized to a common or empty value - if you're doing that, I would explore some sort of custom generative process (building the items of your list as you need them). But, just because I can't think of a good use case for this (where I coul…
Actually [x for x in xrange(..)] will produce only one list. In Python 2.X, range() returns a list while xrange() an iterable. If you just need an iterable, itertools.repeat(0, 100) will do the trick.
Anywho, splitting hairs. Goladus had the most helpful comment (I even learned something).
Re: Code Like a Pythonista: Idiomatic Python
#20In the Java ecosystem, they call it best practices. But of course using the phrase "best practices" these days is considered taboo. Thus the use of the word "idiomatic"?
http://dictionary.reference.com/browse/idiom An idiom is rather a preferred practice. If you take some time to read through some Python source, you'll notice that, although the code uses the exact same constructs that you are used to in Java, it does so in a manner you might find unfamiliar or unpredictable. One example is the use of try/except instead of if/else (beg for forgiveness rather than ask permission). Anot…
from module import *