Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
test interable if len == 2
#1
a function gets a likely iterable. how can i test if it really has a len == 2 when it does not support len(), such as a generator? how does dict() do it? would it be appropriate to just let dict() do the test for me, in a try/except for cases where i need to run certain code when it has the wrong len, making a dictionary i might just throw away? oh wait, i might get an item #0 that is not valid as a dictionary key (cannot be hashed, such as a bytearray).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Why not try to consume 3 elements and store in variables? If you get third value or just zero/one - raise exception or whatever you would do if it is not of len 2.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
can i do that in a comprehension ...consume up to 3 but no more than 3 and collect them in a list? that would simplify a few things.

maybe:
itlist = [next(myiter) for x in range(3)]
could that raise an exception or would it just give me a list of the right length. if the iterable has 351769 possible items i don't want to iterate over all of them ... just 3 at most.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
from itertools import islice

my_iter = range(65536)
my_elements = list(islice(my_iter, 0, 3))

if len(my_elements) != 3:
    raise IndexError('Iterable has to less elements.')
The function islice is like the range function, but for iterators and iterables.
(Iterators are iterable, but iterables does not have to be iterators)
It does not raise an exception, if the iterable is exhausted before the end (3rd element) has been reached.
You have to check, if you've consumed the expected amount of elements.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
because Skaperen wants exactly len 2:
if len(my_elements) != 2:
    raise IndexError('Iterable must have 2 elements')
or

assert len(my_elements) == 2, 'Iterable must have 2 elements.'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,109 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,100 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,543 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020