18

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?

clwen
  • 18,566
  • 30
  • 73
  • 92
  • 2
    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. –  Oct 16 '11 at 15:00
  • 1
    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 Oct 16 '11 at 15:33

7 Answers7

24

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.

andronikus
  • 3,994
  • 1
  • 26
  • 45
  • 2
    +1 and if there's nothing specific (e.g. a generic `slice` function), there are plenty generic names - `xs/ys/zs` for the mathematically inclined (quite popular in some functional programming languages), `objs`, `items`, etc. –  Oct 16 '11 at 15:03
  • 1
    I feel like it's also Pythonic, although I can't point to the exact guideline. – andronikus Oct 16 '11 at 15:05
  • 2
    Out of curiosity, what would you do if you had a list of sheep (or any other thing that doesn't change when going singular -> plural)? – Kevin Jun 18 '18 at 05:32
  • @Kevin Hahaha good point. There are other conventions that can work like `sheep_list` or `sheep_arr`. If it were code that no one else was likely to look at I'd probably use `sheeps` for a laugh. – andronikus Jun 19 '18 at 14:40
  • `flock_of_sheep`, but just `goats`, as `flock_of_goats` is not pythonic. – Wolfgang Kuehn Dec 18 '18 at 17:22
  • What about the usecase of OP? Is `quicksort(ints)` really better than `quicksort(L)`? – kotchwane Apr 17 '21 at 18:09
6
goats

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

Jakob Bowyer
  • 32,237
  • 8
  • 73
  • 89
6

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

Lelouch Lamperouge
  • 7,733
  • 8
  • 44
  • 58
3

Just use lst, or seq (for sequence)

Óscar López
  • 225,348
  • 35
  • 301
  • 374
  • 3
    That's quite cryptic and not very descriptive. (Not my downvote though.) –  Oct 16 '11 at 15:02
3

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.

David Poole
  • 3,262
  • 4
  • 38
  • 34
  • +1 for cow_hash. That sounds delicious. Although I would prefer `_dict` for that variable because they're only called hashes in *every other programming language*. So, not a totally rational reason, but there you go. It's at least easier on new programmers who don't know how a dictionary works deep down. – andronikus Oct 16 '11 at 15:30
2

What about L?

ovgolovin
  • 12,619
  • 5
  • 43
  • 76
  • 9
    From now on, I shall adress you as U for "user". –  Oct 16 '11 at 15:04
  • @delnan Could you please, elaborate why it's a bad practice to use such names? I think this answer is good to be here with the comments why it's not a good idea to use such names. – ovgolovin Oct 16 '11 at 15:07
  • 3
    My comment is meant as example of why it's a bad idea. `L` for a list - like `U` for a user - conveys no real information, is too short and cryptic, and can't be used to differentiate between several such objects (when you have to store two lists in one scope, how do you call them? `L1` and `L2`?). –  Oct 16 '11 at 15:10
  • 3
    I think there are two things going on here: 1. It's hard to remember shorter variable names. Imagine dealing with a long script where things are named `L`, `l`, `s1`, `s2`, etc. `theList`, `theOtherList`, etc would be at least easier to keep track of. 2. It's easier to remember what a variable does if it has a descriptive name. `lines` or `crown_list` or `listOfCountries` all make it clear what is in the list. You can probably guess what I was thinking of, and those were made up! – andronikus Oct 16 '11 at 15:13
  • @delnan OK. Thanks. I voted up your last comment. I'll leave the answer this way with the comments (without editing the answer). – ovgolovin Oct 16 '11 at 15:13
  • 2
    However, using a canonical variable like `i` to index a short loop is OK. If the variable is used on more than one page worth of code, where you can't see its entire lifetime at once, you should give it a sensible name. (Added to my answer.) – andronikus Oct 16 '11 at 15:14
  • This may sound stupid but this was a good answer for me. I wanted a function that removes an element safely from a generic list. `def save_remove(l, item): try: l.remove(item) except ValueError: pass` So when you actually don't know what's in your list l is a good choice :) – Hakaishin Aug 30 '18 at 16:38
1

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.

Achim
  • 14,927
  • 14
  • 75
  • 135