List and tuple methods of Python sequence and precautions

Source: Internet
Author: User
This article mainly introduces the list and tuple methods of Python sequences and their precautions. sequence is a set of ordered objects that can contain one or more elements, there can also be no elements. There are two sequences: list (table) and tuple (tuples ). Sequence

Sequence is a set of ordered objects. A sequence can contain one or more elements, or contain no elements.

The basic data types we mentioned earlier can all be used as sequence objects. The object can also be another sequence. There are two types of sequences: list (table) and tuple (tuples ).
The main difference between list and tuple is that, once created, each element of tuple cannot be changed, and each element of list can be changed.

List

Obtain the number of list elements:

The code is as follows:


>>> Lst = ['update slowness ', 'Python', 5.44, False]
>>> Len (lst)
4

The index starts from 0 when the access is referenced. do not cross-border the index:

The code is as follows:


>>> Lst [0]
'Update slowness'
>>> Lst [1]
'Python'
>>> Lst [2]
5.44
>>> Lst [3]
False
>>> Lst [4]
Traceback (most recent call last ):
File" ", Line 1, in
Lst [4]
IndexError: list index out of range

Use-1 as the index to directly obtain the last element:

The code is as follows:


>>> Lst [-1]
False
>>> Lst [-2]
5.44
>>> Lst [-3]
'Python'
>>> Lst [-4]
'Update slowness'
>>> Lst [-5]
Traceback (most recent call last ):
File" ", Line 1, in
Lst [-5]
IndexError: list index out of range

Because list is a variable ordered table, you can append an element to the end of the list:

The code is as follows:


>>> Lst. append ('Add me one ')
>>> Lst
['Update slowness ', 'Python', 5.44, False, 'Add me one']

Append multiple elements at a time:

The code is as follows:


>>> Lst. extend (['A', 'B', 'C'])
>>> Lst
['Update slowness ', 'Python', 5.44, False, 'Add me A', 'A', 'B', 'C']

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

The code is as follows:


>>> Lst. pop ()
'C'
>>> Lst
['Update slowness ', 'Python', 5.44, False, 'Add me A', 'A', 'B']

Delete the element at the specified position and use the pop (I) method, where I is the index position:

The code is as follows:


>>> Lst. pop (0)
'Update slowness'
>>> Lst
['Python', 5.44, False, 'Add me one ', 'A']

Replace the list element, which can be directly assigned to the corresponding index location:

The code is as follows:


>>> Lst [-1] = '000000'
>>> Lst
['Python', 5.44, False, 'Add me one ', '123']

The list element can also be another list. the inserted list is regarded as only one element:

The code is as follows:


>>> Lst. append (lst1)
>>> Lst
['Python', 5.44, False, 'Add me one ', '123', ['123', 'qwer']
>>> Len (lst)
6

Tuple

Once initialized, Tuple cannot be modified. A string is a special element, so you can perform operations on tuples.

The code is as follows:


>>> Str = 'it's time to go to bed. Good night! '
>>> Print (str [: 7])
It's time to go to bed. Good night.

The immutable tuple makes the code safer because it is immutable. If possible, use tuple whenever possible.

The code is as follows:


>>> Tuple = ('1', '2', '3 ')
>>> Tuple [0] = 6
Traceback (most recent call last ):
File" ", Line 1, in
Tuple [0] = 6
TypeError: 'tuple' object does not support item assignment

Define an empty tuple:

The code is as follows:


>>> Tuple1 = ()
>>> Tuple1
()


Note: to define a tuple with only one element:

The code is as follows:


>>> Tuple2 = (666 ,)
>>> Tuple2
(666,) # Correct
>>> Tuple3 = (666)
>>> Tuple3
666 # Incorrect. only 666 is defined.

Note: Tuple's so-called "unchanged" means that every element of tuple is always pointed.

The code is as follows:


>>> L = ['CCTV-5', 'hi']
>>> Tuple4 = ('uvo', 'hink', l)
>>> Tuple4
('UFOs ', 'hink', ['CCTV-5', 'hi'])
>>> L [1] = 'change first'
>>> Tuple4
('UFOs ', 'hack', ['CCTV-5', 'change first'])

So try to avoid this application.

Summary of the day

Measure the test taker's knowledge about the common methods and precautions of List and Tuple.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.