Anake
Anake

Reputation: 7649

pythonic way of finding a value in a list which hasn't been supplied

Here is an example of what I mean:

a = 0
b = 1

c = range(3)

so I would like to find the missing number in the list which in this case would be 2.

The way I have programmed it at the moment is cumbersome and ugly.

If there was a function opposite to list.append() so that I could remove values from the list instead that would be great to.

Thanks

Upvotes: 1

Views: 941

Answers (3)

intuited
intuited

Reputation: 24044

list.remove will remove the first occurrence of a given value from a list. If you want to specifically remove the last item (the one that was appended), use list.pop.

Upvotes: 1

Mike Lewis
Mike Lewis

Reputation: 64147

Use set difference by converting the list to a set, then preforming the set difference operation.

>>> supplied_list = [0, 1]
>>> list(set(range(3)) - set(supplied_list))
[2]

Upvotes: 2

bradley.ayers
bradley.ayers

Reputation: 38382

Use sets:

>>> a = 0
>>> b = 1
>>> c = range(3)
>>> set(c) - set([a, b])
set([2])

Upvotes: 4

Related Questions