user5307367
user5307367

Reputation: 13

Displaying true output, not stored address

When I ran a code in IPython QTConsole :

-->zip([2,4],[3,1])

I expected it to output [[2,3],[4,1]], but instead it shows

-->

, something like storing address. How can I resolve this?

Upvotes: 1

Views: 22

Answers (1)

Cody Piersall
Cody Piersall

Reputation: 8557

Unfortunately, you can't.

The reason is that zip doesn't always get sequences as arguments. Sequences have a known order, and can be iterated over as many times as you want. Sometimes zip can get generators, where you actually don't know the order, or even how many items are in it up front.

However,

in your specific case, you can do

list(zip([2,4],[3,1]))

To get what you want. If you're only going to iterate over the zipped thing once, you shouldn't call list on it, because then you've done a lot of unnecessary work. It's more efficient to just iterate over the zipped thing.

Upvotes: 2

Related Questions