How to Use List and Tuple in Python?

Python has a built-in data type list: list. list is an orderly collection, you can always add and delete elements.

For example, list all the class name of the class, you can use a list that:

>>> classmates = ['Michael', 'Bob', 'Tracy']
>>> classmates
['Michael', 'Bob', 'Tracy'] 

The variable classmates is a list. Use the len () function to get the number of list elements:
>>> len(classmates)
3 

Use the index to access the elements of each location in the list, remember that the index starts at 0:
>>> classmates[0]
'Michael'
>>> classmates[1]
'Bob'
>>> classmates[2]
'Tracy'
>>> classmates[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
IndexError: list index out of range
When the index is out of range, Python will report an IndexError error, so to ensure that the index does not cross the border, remember that the index of the last element is len (classmates) – 1.

If you want to take the last element, in addition to calculating the index position, you can also use -1 to do the index, directly get the last element:

>>> classmates[-1]
'Tracy'

And so on, you can get the penultimate, the last three:

>>> classmates[-2]
'Bob'
>>> classmates[-3]
'Michael'
>>> classmates[-4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Of course, the last four to cross the border.

list is a variable order table, so you can add elements to the list to the end:

>>> classmates.append('Adam')
>>> classmates
['Michael', 'Bob', 'Tracy', 'Adam']

You can also insert elements into a specified location, such as the index number 1 position:

>>> classmates.insert(1, 'Jack')
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']

To remove the elements at the end of list, use the pop () method:

>>> classmates.pop()
'Adam'
>>> classmates
['Michael', 'Jack', 'Bob', 'Tracy']

To delete the element at the specified position, use the pop (i) method, where i is the index position:

>>> classmates.pop(1)
'Jack'
>>> classmates
['Michael', 'Bob', 'Tracy']

To replace an element with another element, you can directly assign to the corresponding index location:

>>> classmates[1] = 'Sarah'
>>> classmates
['Michael', 'Sarah', 'Tracy']

list of elements inside the data types can also be different, such as:

>>> L = ['Apple', 123, True]

The list element can also be another list, for example:

>>> s = ['python', 'java', ['asp', 'php'], 'scheme']
>>> len(s)
4

To pay attention to s only 4 elements, which s [2] is a list, if the open write easier to understand:

>>> p = ['asp', 'php']
>>> s = ['python', 'java', p, 'scheme']

To get ‘php’ can write p [1] or s [2] [1], so s can be seen as a two-dimensional array, similar to the three-dimensional, four-dimensional … array, but rarely used.

If a list of an element is not, is an empty list, its length is 0:

>>> L = []
>>> len(L)
0

tuple

Another sorted list is called tuple: tuple. tuple and list are very similar, but tuple once the initialization can not be modified, such as the same list of students name:

>>> classmates = ('Michael', 'Bob', 'Tracy')

Now, classmates this tuple can not be changed, it does not append (), insert () this method. Other methods of obtaining elements and list is the same, you can normally use classmates [0], classmates [-1], but can not be assigned to other elements.

What is the meaning of immutable tuple? Because tuples are immutable, the code is safer. If possible, use tuple instead of list to try to use tuple.

tuple trap: When you define a tuple, in the definition of time, tuple elements must be determined, such as:

>>> t = (1, 2)
>>> t
(1, 2)

If you want to define an empty tuple, you can write ():

>>> t = ()
>>> t
()

However, to define a tuple with only one element, if you are so defined:

>>> t = (1)
>>> t
1

The definition is not tuple, is 1 this number! This is because the parentheses () can represent tuples, but also that the mathematical formulas in the brackets, which produces ambiguity, so Python provides that in this case, according to the brackets to calculate the calculation results are naturally 1.

So, only one element of the tuple definition must be added with a comma to eliminate ambiguity:

>>> t = (1,)
>>> t
(1,)

Python will add a comma when you display a tuple with only one element, so you do not misinterpret the brackets in mathematical sense.

Finally look at a “variable” tuple:

>>> t = ('a', 'b', ['A', 'B'])
>>> t[2][0] = 'X'
>>> t[2][1] = 'Y'
>>> t
('a', 'b', ['X', 'Y'])

This tuple defines three elements, namely ‘a’, ‘b’ and a list. Not that tuple once defined after it is immutable? How later changed again?

Do not worry, we first look at the definition of tuple contains three elements:

When we change the list of elements ‘A’ and ‘B’ to ‘X’ and ‘Y’, tuple becomes:

On the surface, tuple elements really changed, but it is not the tuple element, but the list of elements. tuple the beginning of the list of points and did not change to other list, so tuple so-called “invariant” is to say, tuple each element, pointing to never change. That point to the ‘a’, can not be changed to point to ‘b’, pointing to a list, it can not be changed to point to other objects, but the point of the list itself is variable!

Understand the “point to the same”, to create a content is also the same tuple how to do? It must be ensured that each element of the tuple itself can not be changed.