Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List vs tuple, when to use each? [duplicate]

In Python, when should you use lists and when tuples?

Sometimes you don't have a choice, for example if you have

"hello %s you are %s years old" % x 

then x must be a tuple.

But if I am the one who designs the API and gets to choose the data types, then what are the guidelines?

like image 601
flybywire Avatar asked Nov 10 '09 14:11

flybywire


People also ask

When would you use a tuple vs a list?

The key difference between the tuples and lists is that while the tuples are immutable objects the lists are mutable. This means that tuples cannot be changed while the lists can be modified. Tuples are more memory efficient than the lists.

Why would a programmer choose to use a list tuple or set?

Lists are mutable which is one of the reasons why they are so commonly used. Tuple is a collection of values separated by comma and enclosed in parenthesis. Unlike lists, tuples are immutable. The immutability can be considered as the identifying feature of tuples.

Can list and tuple have duplicate values?

Allows duplicate members. Use square brackets [] for lists. Tuple is a collection which is ordered and unchangeable. Allows duplicate members.


2 Answers

There's a strong culture of tuples being for heterogeneous collections, similar to what you'd use structs for in C, and lists being for homogeneous collections, similar to what you'd use arrays for. But I've never quite squared this with the mutability issue mentioned in the other answers. Mutability has teeth to it (you actually can't change a tuple), while homogeneity is not enforced, and so seems to be a much less interesting distinction.

like image 29
Ned Batchelder Avatar answered Sep 28 '22 06:09

Ned Batchelder


Tuples are fixed size in nature whereas lists are dynamic.
In other words, a tuple is immutable whereas a list is mutable.

  1. You can't add elements to a tuple. Tuples have no append or extend method.
  2. You can't remove elements from a tuple. Tuples have no remove or pop method.
  3. You can find elements in a tuple, since this doesn’t change the tuple.
  4. You can also use the in operator to check if an element exists in the tuple.

  • Tuples are faster than lists. If you're defining a constant set of values and all you're ever going to do with it is iterate through it, use a tuple instead of a list.

  • It makes your code safer if you “write-protect” data that does not need to be changed. Using a tuple instead of a list is like having an implied assert statement that this data is constant, and that special thought (and a specific function) is required to override that.

  • Some tuples can be used as dictionary keys (specifically, tuples that contain immutable values like strings, numbers, and other tuples). Lists can never be used as dictionary keys, because lists are not immutable.

Source: Dive into Python 3

like image 93
jldupont Avatar answered Sep 28 '22 06:09

jldupont