5

say i have a deque with values [0,3,5,1,5,8]. I want to save all information about the deque including order, but I have to find if the value 5 is in the deque.

What is some pseudo-code that could determine this?

compStudent
  • 71
  • 2
  • 7

2 Answers2

10

Are you aware of the in operator?

>>> import collections
>>> d = collections.deque([0,3,5,1,5,8])
>>> 5 in d
True
>>> 20 in d
False
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
3

Although most of the time you'd want to use the in operator for membership testing, which deques support, you also have the option of using its count() method — which actually provides more information and so might be more useful depending on exactly what you're trying to accomplish.

>>> import collections
>>> d = collections.deque([0, 3, 5, 1, 5, 8])
>>> d.count(5)
2
>>> d.count(20)
0
>>> bool(d.count(5))
True
>>> bool(d.count(20))
False
martineau
  • 119,623
  • 25
  • 170
  • 301