OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Example of string to list comparison in Python 2 vs Python 3 [closed]

  • Thread starter Thread starter jsaintrocc
  • Start date Start date
J

jsaintrocc

Guest
I'm researching the differences between Python 2 and Python 3. It's been mentioned that string-to-list comparisons do not work in Python 3 as they're of limited value. I'm trying to find an example of this. To clarify I'm expecting a comparison between a string and a list in Python 3 to throw an error but it doesn't do that, at least not for '=='. It does do that for '>='. Why doesn't it throw an error for '=='? Here's what I've tried:

Code:
Python 2.7.18 (default, Jun 23 2024, 08:39:49)
[GCC Apple LLVM 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['a', 'b', 'c']
>>> print 'abc' == mylist
False
>>>

Shouldn't ^^^ return True because 3 == 3? Isn't the behavior of Python 2 to compare the lengths of the list and the string?

Code:
Python 3.12.0 (main, Nov 29 2023, 07:31:13) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['a', 'b', 'c']
>>> print('abc' == mylist)
False

I was expecting Python 3 to get mad at me for this. Why does it throw an Error for '>=' and not '=='?

Code:
Python 3.12.0 (main, Nov 29 2023, 07:31:13) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['a', 'b', 'c']
>>> print('abc' >= mylist)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>=' not supported between instances of 'str' and 'list

Ok that seems right but why is == supported?
<p>I'm researching the differences between Python 2 and Python 3. It's been mentioned that string-to-list comparisons do not work in Python 3 as they're of limited value. I'm trying to find an example of this. To clarify I'm expecting a comparison between a string and a list in Python 3 to throw an error but it doesn't do that, at least not for '=='. It does do that for '>='. Why doesn't it throw an error for '=='? Here's what I've tried:</p>
<pre class="lang-py prettyprint-override"><code>Python 2.7.18 (default, Jun 23 2024, 08:39:49)
[GCC Apple LLVM 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['a', 'b', 'c']
>>> print 'abc' == mylist
False
>>>
</code></pre>
<p>Shouldn't ^^^ return True because 3 == 3? Isn't the behavior of Python 2 to compare the lengths of the list and the string?</p>
<pre class="lang-py prettyprint-override"><code>Python 3.12.0 (main, Nov 29 2023, 07:31:13) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['a', 'b', 'c']
>>> print('abc' == mylist)
False
</code></pre>
<p>I was expecting Python 3 to get mad at me for this. Why does it throw an Error for '>=' and not '=='?</p>
<pre class="lang-py prettyprint-override"><code>Python 3.12.0 (main, Nov 29 2023, 07:31:13) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = ['a', 'b', 'c']
>>> print('abc' >= mylist)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>=' not supported between instances of 'str' and 'list
</code></pre>
<p>Ok that seems right but why is <code>==</code> supported?</p>
 

Latest posts

Top