Python Forum
how to test if something is a sequence?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to test if something is a sequence?
#1
how to test if something is a sequence (as opposed to merely a list)?
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
Use perhaps collections.abc.Iterable
isinstance(obj, Iterable)
Documentation Wrote:The only reliable way to determine whether an object is iterable is to call iter(obj)
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
To test if something is a sequence rather than just a list, you can check if it has a specific order or arrangement that follows a pattern or progression. Sequences typically exhibit a logical or numerical progression, whereas lists may not necessarily have a defined order. It's about identifying the underlying structure and patterns within the data.
Reply
#4
There is from collections.abc import Sequence.
In Python, a sequence is an abstract data type that represents an ordered collection of items.
from collections.abc import Sequence

def is_sequence(obj):
    return isinstance(obj, Sequence)
>>> example_list = [1, 2, 3]
>>> is_sequence(example_list)
True
>>> example_string = "123"
>>> is_sequence(example_string)
True
Dictionaries and sets return False because they are not considered sequences in the context of the Sequence abstract base class.
>>> example_dict = {1: "a", 2: "b", 3: "c"}
>>> is_sequence(example_dict)
False
>>> example_set = {1, 2, 3}
>>> is_sequence(example_set)
False
Reply
#5
(Jun-09-2024, 11:54 PM)Skaperen Wrote: how to test if something is a sequence (as opposed to merely a list)?

List is sequence so there is no opposition.

Python documentation > Glossary > sequence:

Quote:sequence
An iterable which supports efficient element access using integer indices via the __getitem__() special method and defines a __len__() method that returns the length of the sequence. Some built-in sequence types are list, str, tuple, and bytes. Note that dict also supports __getitem__() and __len__(), but is considered a mapping rather than a sequence because the lookups use arbitrary immutable keys rather than integers. /.../
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
does that mean i can't use an int to index a mapping? or is an int mutable (i don't think so)?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
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,386 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,268 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,654 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