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: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

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