Sometimes, we want to create a linked list with Python.
In this article, we’ll look at how to create a linked list with Python.
How to create a linked list with Python?
To create a linked list with Python, we can use a deque.
For instance, we write:
from collections import deque
d = deque([1, 2, 3, 4])
print(d)
for x in d:
print(x)
print(d.pop(), d)
We create a deque with deque([1, 2, 3, 4])
.
Next, we loop through the items in d
and print them.
Then we call d.pop
to remove the last entry from the deque and print the new value of d
.
Therefore, we see:
deque([1, 2, 3, 4])
1
2
3
4
4 deque([1, 2, 3])
printed.
Conclusion
To create a linked list with Python, we can use a deque.