close

[solved] TypeError: ‘<' not supported between instances of 'tuple' and 'str'

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: ‘<‘ not supported between instances of ‘tuple’ and ‘str’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: ‘<‘ not supported between instances of ‘tuple’ and ‘str’ Error Occurs?

Today I get the following error TypeError: ‘<‘ not supported between instances of ‘tuple’ and ‘str’ in python.

How To Solve TypeError: ‘<‘ not supported between instances of ‘tuple’ and ‘str’ Error ?

  1. How To Solve TypeError: '

    To Solve TypeError: '<' not supported between instances of 'tuple' and 'str' Error The error is thrown because you are creating inner nodes in (priority, (node, node)) form.

  2. TypeError: '

    To Solve TypeError: '<' not supported between instances of 'tuple' and 'str' Error The error is thrown because you are creating inner nodes in (priority, (node, node)) form.

Solution 1

The error is thrown because you are creating inner nodes in (priority, (node, node)) form. For equal priorities, Python then tries to compare a symbol from a leaf node (so the second element in a (priority, symbol) node tuple) with the (node, node) tuple from an inner node:

>>> inner = (combFreq, leastTwo)
>>> inner
(2, ((1, 'b'), (1, 'd')))
>>> theRest[1]
(2, 'c')
>>> theRest[1] < inner
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'tuple'

For building a huffman tree, if you want to sort your array of nodes, you only really need to sort on the priority, ignoring the rest of the tuples (symbols or child nodes):

tuples.sort(key=lambda t: t[0])

With that correction, your buildTree() function produces a tree:

>>> buildTree([(1, 'b'), (1, 'd'), (1, 'g'), (2, 'c'), (2, 'f'), (3, 'a'), (5, 'e')])
(15, ((6, ((3, 'a'), (3, ((1, 'g'), (2, 'c'))))), (9, ((4, ((2, 'f'), (2, ((1, 'b'), (1, 'd'))))), (5, 'e')))))

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read