Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between lists and tuples in Python?

Which is more efficient? What is the typical use of each?

like image 955
banx Avatar asked Jul 28 '10 22:07

banx


3 Answers

Lists are mutable sequences, with lots and lots of methods (both mutating and non-mutating ones), that are most often used as general purpose containers (their items can be objects of any types at all, although it's sometimes considered better style for lists to have items that are of the same type or types to be used equivalently).

Tuples are immutable sequences, with very few methods (all non-mutating special ones), that are most often used when you need immutability for the purpose of using a container as an item in a set or a key in a dict (though the items will also have to be immutables -- e.g. strings, numbers, or other nested tuples, for this to work). Their items can be objects of any types at all and it's perfectly normal for tuples to have items of many different and distinct types.

There are a few cases in which either a tuple or a list would serve just as well, and in such few cases the fact that tuples are a bit smaller and faster to build can be used to sway the decision in favor of tuples. For example, when a function needs to return multiple results, it's most normal to use

return fee, fie, foo, fum

i.e., return a tuple with the four items in question, rather than

return [fee, fie, foo, fum]

i.e., return a list with the four items -- besides (small gains in) performance, the "return a tuple" common idiom also deals with the issue that often the multiple results being returned are not of the same nor interchangeable types, so, stylistically speaking, using a list might be considered a more dubious choice anyway.

A useful variant of tuple is its sub-type collections.namedtuple (requires Python 2.6 or better) which lets you access items by name (with attribute syntax) as well as by index (the normal way). For example, with an import collections at the top of the module, the above return statement might become...

freturn = collections.namedtuple('freturn', 'fee fie foo fum')

def f():
  ...
return freturn(fee, fie, foo, fum)

Now, the caller of f() could use its return value as a tuple, just as before, but would gain nice alternatives such as...:

r = f()
print r.fie

in lieu of the less immediately clear and readable

print r[1]

It's important to note that a named tuple subclass made with collections.namedtuple has essentially no extra overhead compared with using a tuple directly, or, as the docs put it,

they are lightweight and require no more memory than regular tuples.

like image 160
Alex Martelli Avatar answered Nov 11 '22 02:11

Alex Martelli


A list is mutable, you can add elements to it. A tuple isn't, which means it's (slightly) more efficient. Tuples are also hashable, so can be used as e.g. the key in a dictionary.

Read this.

like image 33
Katriel Avatar answered Nov 11 '22 03:11

Katriel


Lists are mutable (can be changed), tuples are immutable. Typical use: it sounds rather trite but you use lists when you need to change the values. Tuples are generally a little more efficient because of their immutability (unless you are using them like lists and duplicating them a lot...)

like image 42
Bryan Oakley Avatar answered Nov 11 '22 03:11

Bryan Oakley