-1

Say I have an array of integer values as such:

foo = [1, 5, 19, 27]

Now say I generate a list of values from 0 to 29 as such:

bar = range(30)

I want to print the values of bar without printing the values listed in foo.

How would I go about doing that in Python 2.7?

[EDIT]

I figured it out from this question python - Removing duplicates in lists

It turns out that my question is the same as "How to remove duplicates in lists" but I didn't figure that out when I was asking the question or searching for other questions similar to mine.

To put this question to rest, here's the answer:

list(set(bar) - set(foo))

That prints the values of bar without printing the values of foo.

Community
  • 1
  • 1
  • I have already tried itertools.product and I've already tried nesting for loops to get what I'm looking for. The issue with itertools.product is that it returns duplicate values which could pose a memory problem for rather large lists of integers. The issue with nested for loops is that it takes far too long to parse through a rather large list of integers. – KimChoJapFan Jul 25 '16 at 10:13
  • try **filter(function, iterable)** https://docs.python.org/2/library/functions.html – Take_Care_ Jul 25 '16 at 10:13
  • @KimChoJapFan Can you comment on the bounds of the range? – Shubham Jul 25 '16 at 11:28
  • @ShubhamBhattar I suppose I can: range(30) is equal to all values between 0 and 29. – KimChoJapFan Jul 25 '16 at 11:48

1 Answers1

0

Good that you figured out the way to solve this. I want to tell you about another way to achieve this, which would work in O(n) time but for this the maximum size of the range has to be approximatey <= 10^7.

We can create a auxiliary array containing 0's. Then in one iteration across foo we can flag (mark as 1) in the bar array whether this value is to be printed or not. The remaining array which is filled with 0's are the answer. To put this in code:

n = 30
foo = [1, 5, 19, 27]
bar = [0]*n

for i in foo:
    bar[i] = 1

for i in xrange(n):
    if bar[i] == 0:
        print i
Shubham
  • 2,847
  • 4
  • 24
  • 37