Common list and tuple methods and considerations for Python sequences

  • 2020-04-02 14:29:43
  • OfStack

The sequence sequence

A sequence(sequence) is a collection of sequential objects. A sequence can contain one or more elements, or none at all.

All of the basic data types we talked about earlier can be used as objects in a sequence. An object can also be another sequence. There are two types of sequences: a list (table) and a tuple (tuple).
The main difference between a list and a tuple is that, once established, the individual elements of a tuple cannot be changed, while the individual elements of a list can be changed again.

The List

Get the number of list elements:


>>> lst=[' Update slow ','python',5.44,False]
>>> len(lst)
4

Index starts at 0 when reference access, be careful not to cross the line:


>>> lst[0]
' Update slow '
>>> lst[1]
'python'
>>> lst[2]
5.44
>>> lst[3]
False
>>> lst[4]
Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    lst[4]
IndexError: list index out of range

Index with -1, get the last element directly:


>>> lst[-1]
False
>>> lst[-2]
5.44
>>> lst[-3]
'python'
>>> lst[-4]
' Update slow '
>>> lst[-5]
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    lst[-5]
IndexError: list index out of range

Since list is a mutable ordered table, you can append elements to the end of list:


>>> lst.append(' Add me in ')
>>> lst
[' Update slow ', 'python', 5.44, False, ' Add me in ']

Append more than one element at a time:


>>> lst.extend(['A','B','C'])
>>> lst
[' Update slow ', 'python', 5.44, False, ' Add me in ', 'A', 'B', 'C']

To delete the element at the end of the list, use the pop() method:


>>> lst.pop()
'C'
>>> lst
[' Update slow ', 'python', 5.44, False, ' Add me in ', 'A', 'B']

Deletes the element in the specified position, using the pop(I) method, where I is the index position:


>>> lst.pop(0)
' Update slow '
>>> lst
['python', 5.44, False, ' Add me in ', 'A']

List element replacement, can be directly assigned to the corresponding index position:


>>> lst[-1]='100'
>>> lst
['python', 5.44, False, ' Add me in ', '100']

The list element can also be another list, and the inserted list only counts as one element:


>>> lst.append(lst1)
>>> lst
['python', 5.44, False, ' Add me in ', '100', ['666', 'QWER']]
>>> len(lst)
6

A Tuple

Once a Tuple is initialized, it cannot be modified. A string is a special element, so you can perform tuple related operations.


>>> str=' It's time for bed. Good night! '
>>> print (str[:7])
Time for bed. Good night

The point of an immutable tuple is that code is safer because it is immutable. If you can use a tuple, use a tuple.


>>> tuple=('1','2','3')
>>> tuple[0]=6
Traceback (most recent call last):
  File "<pyshell#62>", line 1, in <module>
    tuple[0]=6
TypeError: 'tuple' object does not support item assignment

Define an empty tuple:


>>> tuple1=()
>>> tuple1
()

Note that to define a tuple with only 1 element:

>>> tuple2=(666,)
>>> tuple2
(666,)              # The correct
>>> tuple3=(666)
>>> tuple3
666                 # Wrong, just defined 666 The number of

Note: when a Tuple says "invariant," it means that every element of a Tuple has the same point forever.


>>> l=['CCTV-5','HI']
>>> tuple4=('UFO','HACK',l)
>>> tuple4
('UFO', 'HACK', ['CCTV-5', 'HI'])
>>> l[1]=' Let me change that '
>>> tuple4
('UFO', 'HACK', ['CCTV-5', ' Let me change that '])

So try to avoid it.

The summary

Master the common methods and considerations for lists and tuples.


Related articles: