2

To load a list into a queue in Python, I found this code snippet which failed to work. No items were added to the queue:

from queue import Queue
my_list = [1,2,3,4,5,6,7,8,9,10]
q = Queue()

# This code doesn't work
map(q.put, my_list)
q.qsize() # Returns zero, which is unexpected

The more verbose solution:

for num in my_list:
    q.put(num)
print(q.qsize())  # returns 10 as expected

works as expected. What am I missing here?

max
  • 4,141
  • 5
  • 26
  • 55
  • 2
    Using map like this is not idiomatic python. I would stick to the for-loop because you'll need to evaluate the map anyway. – Exelian Jul 30 '18 at 14:58

3 Answers3

7

map(q.put, my_list) just returns an iterator. Unless you iterate through it, your queue q wont be populated

>>> q = Queue()
>>> itr = map(q.put, my_list)
>>> q.qsize()
0
>>> _ = list(map(q.put, my_list))
>>> q.qsize()
10
Sunitha
  • 11,777
  • 2
  • 20
  • 23
0

That is not how map works:

from queue import Queue
l = [i for i in range(10)]
q = Queue()
x = map(q.put, l)
q.qsize()
# Output: 0
for _ in x:
    pass
q.qsize()
# Output: 10

You can get what you want using a ThreadPool:

from queue import Queue
from multiprocessing.pool import ThreadPool
l = [i for i in range(10)]
p = ThreadPool()
q = Queue()
_ = p.imap_unordered(q.put, l)
q.qsize()
# Output: 10

If you want other built-ins:

# list comprehension (essentially what map is equivalent to)
_ = [q.put(i) for i in l]

# the `any` method:
_ = any(q.put(i) for i in l)
pstatix
  • 3,611
  • 4
  • 18
  • 40
  • 1
    In other words, in Python 3.x `map()` returns a lazy iterator, rather than a list as in Python 2.x, so you have to explicitly iterate it to get it to do anything. The "more verbose" version is a better idea anyway, as there's absolutely no point in creating this sequence of `None`s. – jasonharper Jul 30 '18 at 15:03
0

I am not sure why that is not working for you. Maybe your version of python, I am on 2.7.6

from queue import Queue
my_list = [1,2,3,4,5,6,7,8,9,10]
q = Queue()

# This code doesn't work
map(q.put, my_list)
q.qsize()

print q.qsize() # 10

while not q.empty():
    print q.get()
Kenan
  • 13,156
  • 8
  • 43
  • 50