In Python, we can add items to deque by using some of deque methods. The deque stands for Double Ended Queue which is an one-dimensional data structure like list in python. In this data structure, we can perform insertion and deletion operations from both ends.
Related: Python Deque Methods
In this article, we will see how to add items to a deque with the following methods.
Add single item at right using append()
Add single item at left using appendleft()
Add multiple items at left using extendleft()
Add multiple items at right using extend()
Add element at particular position using insert()
It is important to import deque from the collections module.
1. Quick Examples of Add Items to Deque
Following are quick examples of how to add items to a deque in python.
# below are some quick examples.
# Using append() to add single item at right
deque1.append(‘Germany’)
# Using appendleft() to add single item at left
deque1.appendleft(‘Germany’)
# Using extend() to add item at right
deque1.extend([‘Europe’, ‘Spain’, ‘France’])
# Using extendleft() to add item at left
deque1.extendleft([‘Europe’, ‘Spain’, ‘France’])
# Insert at first using insert()
deque1.insert(0,’Germany’)
# Insert at last using insert()
deque1.insert(len(deque1),’Germany’)
# Insert at particular position using insert()
deque1.insert(5,’Germany’)
print(deque1)
2. Add Items at Right of Python Deque using append() & extend()
Here, we will add items at the right side of the deque using append() and extend() methods. The append() will add only single item to the deque and extend() will add multiple items to the deque.
2.1 Examples
Example 1: Let’s create deque with 6 countries and add an item to the right of the deque.
import collections
# Declaring deque with 6 countries
deque1 = collections.deque([‘China’, ‘Russia’, ‘Italy’, ‘England’, ‘UK’, ‘USA’])
print(deque1)
# Using append() add single item to right of deque
deque1.append(‘Germany’)
print(deque1)
Yields below output.
We are adding ‘Germany’ to the deque and it is added at the last (right). Now there are 7 countries in the deque.
Example 2: Let’s add 3 items at a time to the right of the deque using extend() method.
# Using extend() add multiple items to right of deque
deque1.extend([‘Europe’, ‘Spain’, ‘France’])
print(deque1)
Yields below output.
We have added 3 countries – [‘Europe’, ‘Spain’, ‘France’] to the deque and they are inserted at right. Finally the deque holds 9 countries.
3. Add Items at Left of Python Deque using appendleft() & extendleft()
Here, we will add items at the left side (start) of the deque using appendleft() and extendleft() methods. In Python appendleft() will add only a single item to the deque and extendleft() will add multiple items to the deque.
The last element will be added to the first position.
3.1 Examples
Example 1: Let’s add an item at first position of the Python deque.
# Using appendleft() add single item at lest
deque1.appendleft(‘Germany’)
print(deque1)
# Output:
# deque([‘Germany’, ‘China’, ‘Russia’, ‘Italy’, ‘England’, ‘UK’, ‘USA’])
We are adding ‘Germany’ to the deque and it is added at the first (left). Now there ate 7 countries in the deque.
Example 2: Let’s add 3 items at a time to the left of the deque using extendleft() method.
# Using extendleft() add multiple items at left side
deque1.extendleft([‘Europe’, ‘Spain’, ‘France’])
print(deque1)
# Output:
# deque([‘France’, ‘Spain’, ‘Europe’, ‘China’, ‘Russia’, ‘Italy’, ‘England’, ‘UK’, ‘USA’])
We are adding 3 countries – [‘Europe’,’Spain’,’France’] to the deque and they are inserted at left. Finally the deque hold 9 countries.
4. Add Items to Python Deque at Particular Position
In Python insert() method is used to add items at a particular position of the deque. It will take two parameters. The first parameter is the index position in which the item has to be inserted and the second parameter is the item.
Indexing in deque starts from 0.
4.1 Examples
Example 1: Let’s add an item at the first position of the Python deque.
# Using insert() add item at specified position
deque1.insert(0,’Germany’)
print(deque1)
# Output:
# deque([‘Germany’, ‘China’, ‘Russia’, ‘Italy’, ‘England’, ‘UK’, ‘USA’])
We specified 0 as index_pos. So the string – ‘Germany’ is inserted at the first position.
Example 2: Let’s add an item at the last position of the deque.
# Using insert() to add item at last position
deque1.insert(len(deque1),’Germany’)
print(deque1)
# Output:
# deque([‘China’, ‘Russia’, ‘Italy’, ‘England’, ‘UK’, ‘USA’, ‘Germany’])
We specified the size of deque as index_pos using the len() function. so the string – ‘Germany’ is inserted at the last position.
Example 3: Let’s create a deque with 6 countries and add an item at 4th position in Python.
# Using insert()
deque1.insert(3,’Germany’)
print(deque1)
# Output:
# deque([‘China’, ‘Russia’, ‘Italy’, ‘Germany’, ‘England’, ‘UK’, ‘USA’])
We specified index_pos as 3. So the string – ‘Germany’ is inserted at fourth position (jndex=3).
5. Conclusion
In this article, you have learned how to add elements or items to deque in Python. As the deque takes elements from both ends, append() and extend() methods will add elements at the right side of the deque, and appendleft() and extendleft() methods will add elements at the left side of the deque. If you want to add an item at a particular position, you can use the insert() method.
In Python, we can add items to deque by using some of deque methods. The deque stands for Double Ended Queue which is an one-dimensional data structure like list in python. In this data structure, we can perform insertion and deletion operations from both ends. Related: Python Deque Methods In this article, we will see how Read More Python, Python Tutorial, Python Deque