Deque In Python: 6 Things You (2 Min) Need To know | Collections Module

preview_player
Показать описание
In this tutorial, you'll learn how to use deque in Python from the collections module.



Video Transcript:

Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn six ways to use a deck from the collections module in Python. Number one, a deck is a list like a sequence optimized for data access near its endpoints.

It accepts an errorable such as a list tuple string or a dictionary with the items method. Number two, you can use pop left or a pen left method to remove or add from the left end of the sequence. Here I'm first using the pop left method to remove the first item from the left then using the append left method to add property two to the left.

Number three, you can use the pop or append methods to remove or add items to the right end of the sequence. In this example, I'm using the pop method to remove sales than using the append method to add sales 3 to the right end of the sequence. Note, you cannot remove arbitrary items using pop because the deck is implemented as a doubly linked list.

Number four is to insert an item at a specific index using the insert method. If you wanted to remove an item by value, use the remove method alternatively. You can use the Del keyword to delete an item by index.

Number five, use the rotate method to rotate the items to the left or to the right. If you don't pass anything to the rotate method, the items will be rotated to the right by one. If you pass minus one then the items will be rotated to the left by one.

Last but not the least number six. Pass the Maxlen argument in the constructor to specify the maximum length of the deck. On line 48, the property item will get dropped because I specified a max length of 2.

On line 50, the customer will get dropped because I'm appending taxes to the right end of the sequence. On line 52, taxes will be dropped because I'm a pending client to the left. There you have it.

Make sure you like, subscribe, and turn on the notification bell. Until next time.



from collections import deque

# 1. A deque is a list-like sequence optimized for data accesses near its endpoints
# Accepts an iterable
table_deq1a = deque(["property", "customer", "sales"])
print("1a.", table_deq1a)
table_deq1b = deque(("property", "customer", "sales"))
print("1b.", table_deq1b)
table_deq1c = deque("property")
print("1c.", table_deq1c)
table_deq1d = deque({"property": 5, "customer": 6}.items())
print("1d.", table_deq1d)

# 2. Use popleft()/appendleft() to remove/add from the left end of the sequence
table_deq2 = deque(["property", "customer", "sales"])
print("2a.", table_deq2)
print("2b.", table_deq2)
print("2c.", table_deq2)

# 3. Use pop()/append() to remove/add from the right end of the sequence
# Deque is implemented as a doubly linked list, so you can't remove arbitrary items using pop
table_deq3 = deque(["property", "customer", "sales"])
print("3a.", table_deq3)
print("3b.", table_deq3)
print("3c.", table_deq3)

# 4. insert(), remove(), del
table_deq4 = deque(["property", "customer", "sales"])
print("4a.", table_deq4)
print("4b.", table_deq4)
del table_deq4[1] # delete by index
print("4c.", table_deq4)

# 5. rotate() to rotate the items
table_deq5 = deque(["property", "customer", "sales"])
print("5a.", table_deq5)
print("5b.", table_deq5)

# 6. Use maxlen argument in the constructor to specify the maximum length of the deque
table_deq6 = deque(["property", "customer", "sales"], maxlen=2)
print("6a.", table_deq6)
print("6b.", table_deq6)
print("6c.", table_deq6)
Рекомендации по теме
welcome to shbcf.ru