list are three common build-in ordered collection data types in Python. Those sequence data types share some common operations.
|reference: Operations on Any Sequence in Python (interactivepython.org), 5.6 Sequence Types|
|slicing with step k||
Those 3 sequence data types,
tuple are immutable;
list is mutable. Above common operations can be used on both mutable and immutable data types.
Before we talk about the special methods for each data type, I'd like to talk about
In Python, both
tuple are heterogenous collections (although
ist is intended to be homogeneous sequences); however, there's no special methods for
tuple is immutable.
So, why using
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.
|source: Python Memory Usage: What values are taking up so much memory?|
|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
- 9. Tuples (openbookproject.net)
- What's the difference between list and tuples in Python? (stackoverflow.com)
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
|Delete item with k step|
|Pop||You can also assign the index of the item you want to pop|
More examples please check:
In Python, both
sorted() have three arguments:
reverse; however, using
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
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() 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
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.
[:] 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:
- Python: copying a list the right way
- Copying Lists in Python (www.tomleo.com)
- Python Programming/Lists (en.wikibooks.org)
- Python Sorting (developers.google.com)
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.
|source: 5.6.1. String Methods|
|Expand Tabs to Spaces|
Well...that's it! I think this article is long enough.