Detailed description of deque modules in Python

Source: Internet
Author: User
Tags for in range

The Deque module is an item in the Python standard library collections, which provides a sequence that can be manipulated at both ends, which means that you can perform add or delete operations before and after the sequence.

1. Create the deque sequence:

 from Import dequed=deque ()

The 2.deque provides a list-like method of operation:

d=deque () d.append (3) d.append (8) d.append (1)

So at this point

D=deque ([3,8,1]), Len (d) =3,d[0]=3,d[-1]=1

3. Use pop on both ends:

D=deque (' 12345 ')

So

D=deque (['1'2"3"  4"5"])

D.pop () throws the ' 5 ', D.leftpop () throws the ' 1 ', and the Visible default pop () throws the last element.

4. Limit the length of the deque

D=deque (maxlen=20) for in range:    d.append (str (i))

At this point, the value of D is

D=deque (['Ten',' One',' A',' -',' -',' the',' -',' -',' -',' +',' -',' +',' A',' at',' -',' -',' -',' -',' -',' in'], maxlen=20)

, it is visible that items on the other side are automatically deleted when the limit length of deque increases by more than the limit number of items.

5. Add list items to deque:

D=deque ([1,2,3,4,5]) d.extend ([0])

So at this point

D=deque ([1,2,3,4,5,0])

D.extendleft ([6,7,8])

At this time

D=deque ([8, 7, 6, 1, 2, 3, 4, 5, 0])

Detailed description of deque modules in Python

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.