# Understand Common Sequence Data Types in Python – String, Tuple, and List

string, tuple, and list are three common build-in ordered collection data types in Python. Those sequence data types share some common operations.

Common Sequence Operations in Python
Name Operator Example
reference: Operations on Any Sequence in Python (interactivepython.org), 5.6 Sequence Types
indexing [n]
concatenation +
repetition *
membership in
length len()
slicing with step k [i:j:k]
slicing [i:j]
Minimum min(data)
Maximum max(data)
Index data.index(sub[, start[, end]])
Count data.count(i)

Those 3 sequence data types, string and tuple are immutable; list is mutable. Above common operations can be used on both mutable and immutable data types.

#### Tuple

Before we talk about the special methods for each data type, I'd like to talk about tuple first.

In Python, both list and tuple are heterogenous collections (although ist is intended to be homogeneous sequences); however, there's no special methods for tuple because tuple is immutable.

So, why using tuple?

Gred Wilson suggested that tuples should be one of the things Python 3000 could leave out, but Phillip Eby pointed out that tuples are not just constant lists but heterogeneous data structures.

Tuples are not constant lists -- this is a common misconception. Lists are intended to be homogeneous sequences, while tuples are hetereogeneous data structures.

If you treat tuple as a constant list, then you will probably be very confused about it; but if you understand tuple as a data structure, just like JSON, it would be much easier to understand the purpose of using tuple.

tuple can be very useful if you want to store data, and it takes less memory than list because it is immutable data type.

Measured in bytes using Python 2.5 in 64-bit Ubuntu Linux
data type bytes
source: Python Memory Usage: What values are taking up so much memory?
int 24
float 24
tuple 63
list 101
dict 298
old-style class 345
new-style class 336
subclassed tuple 79
Record 79
Record with old class mixin 79
Record with new class mixin 79

If you want to know more things about tuples, here are more related articles about tuple.

Releated Articles:

#### List

In tuple section, we mentioned list a little bit. List is an ordered heterogenous collection data type which starts counting with 0. Here are some common methods for list:

Methods for list
Name Operator Example
Delete item
Delete item with k step
Append
Extend
Insert
Pop You can also assign the index of the item you want to pop
Reverse
Remove Item
Sorting More examples please check: sort() method

#### sort() method

In Python, both sort() and sorted() have three arguments: cmp, key, and reverse; however, using key and reverse is more preferred because they are much faster than cmp. When Python sort a list, cmp will be called multiple times for each list element, but key and reverse will only touch each element once. (please refer to this document)

Instead of using data.sort(), you can also use sorted(data). The difference between data.sort() and sorted(data) is data.sort() will modify the original data, but sorted(data) will return the new sorted data.

cmp specifies a comparison function of two arguments. This comparison function will compare whether the first argument is smaller than, equal to, or larger than the second argument, and this function will return a negative, zero, or positive number depends on the comparing result.

Here is the simplest example for using cmp which shows the logic of how cmp doing soring base on the returning result.

key specifies a function of one argument and the default value is None. The key function takes 1 argument and returns 1 value.

reverse is a Boolean value, which tells sort() to reverse the result or not. This argument can also be used with cmp or key.

#### list and reference

When you use list to do some operations, you might need to be very carefule about the reference issue.

But if you do something like this:

If you put your list into another list, Python copies each item by reference. If you don't want Python copies items by reference, you can simply use [:] to copy the list.

However, [:] is just a shallow copy, which can only copy the first level of elements and will not recursively duplicate the elements within itself. Let's just use the above example and see what will happen with nested list.

To copy a nested list with reference, you will need to use copy.deepcopy(). Here are more related articles of copying lists in Python:

Related Articles:

#### String

Like tuple, string is an immutable sequence data type. Other than the common sequence collection data type operations we just introduced, string has more methods that can help you easily deal with the strings, but I am not going to list all of them. If you are want to know more information about string methods, please check the official Python document.

Common String Methods in Python
Name Method Example
source: 5.6.1. String Methods
Capitalize
Center
Ends With
Expand Tabs to Spaces
Find
Format
Justified
Join
Partition
Replace
Strip
Split