Fix: TypeError: unhashable type: slice


Example Code:
data_list = [slice(1, 2), slice(3, 4)]
data_set = set(data_list)
Error:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[92], line 2
      1 data_list = [slice(1, 5), slice(5, 10)]
----> 2 data_set = set(data_list)

TypeError: unhashable type: 'slice'

    Reason for the Error:

    Here we are trying to convert a list of slices to a set, this is not possible because slices are mutable whereas sets are a hash-based data structure that needs their elements to be immutable.

TypeError- unhashable type- slice

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap