Categories
Python Answers

How to create a linked list with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *