15
votes

Is it better not to name list variables "list"? Since it's conflicted with the python reserved keyword. Then, what's the better naming? "input_list" sounds kinda awkward.

I know it can be problem-specific, but, say I have a quick sort function, then quick_sort(unsorted_list) is still kinda lengthy, since list passed to sorting function is clearly unsorted by context.

Any idea?

7
Nitpick: list isn't a "reserved keyword" (I assume you mean "reserved identifier" or "keyword"), it's just a regular identifier... it just happens to be used by a builtin type. - user395760
Python does tend to get pretty wordy at times. The designers generally choose readability over conciseness, although take a look at list comprehensions for a counterexample. So don't worry too much about how much you have to type, because it's hard to fight and good for readability. - andronikus

7 Answers

23
votes

I like to name it with the plural of whatever's in it. So, for example, if I have a list of names, I call it names, and then I can write:

for name in names:

which I think looks pretty nice. But generally for your own sanity you should name your variables so that you can know what they are just from the name. This convention has the added benefit of being type-agnostic, just like Python itself, because names can be any iterable object such as a tuple, a dict, or your very own custom (iterable) object. You can use for name in names on any of those, and if you had a tuple called names_list that would just be weird.

(Added from a comment below:) There are a few situations where you don't have to do this. Using a canonical variable like i to index a short loop is OK because i is usually used that way. If your variable is used on more than one page worth of code, so that you can't see its entire lifetime at once, you should give it a sensible name.

6
votes
goats

Variable names should refer what they are not just what type they are.

6
votes

Python stands for readability. So basically you should name variables that promote readability. See PEP20.
You should only have a general rule of consistency and should break this consistency in the following situations:

  1. When applying the rule would make the code less readable, even for someone who is used to reading code that follows the rules.

  2. To be consistent with surrounding code that also breaks it (maybe for historic reasons) -- although this is also an opportunity to clean up someone else's mess (in true XP style)

Also, use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.
All this is taken from PEP 8

3
votes

I use a naming convention based on descriptive name and type. (I think I learned this from a Jeff Atwood blog post but I can't find it.)

goats_list
for goat in goats_list : 
    goat.bleat()

cow_hash = {}

etc.

Anything more complicated (list_list_hash_list) I make a class.

2
votes
1
votes

Just use lst, or seq (for sequence)

1
votes

Why not just use unsorted? I prefer to have names, which communicate ideas, not data types. There are special cases, where the type of a variable is important. But in most cases, it's obvious from the context - like in your case. Quick sort is obviously working on a list.