Python Forum
TypeError: 'NoneType' object is not subscriptable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: 'NoneType' object is not subscriptable
#1
Bug 
import re def rearrange_name(name): result = re.search(r"^([\w .]*), ([\w .])$", name) return "{} {}".format(result[2], result[1])
When I import from the directory the rearrange_name method and run rearrange_name("Lovelace, Ada") I get the following error:
TypeError: 'NoneType' object is not subscriptable

Any insights on how to actually solve this ? Wall
Reply
#2
You are getting this message because result is None, so you are trying to access None[2] and Python does not allow that.
Larz60+ and TheLummen like this post
Reply
#3
Thank you ! Can you please give me a working example ?
I'm quite new to this and been studying the Google IT automation with Python on Coursera and on one of the videos I replicated the above code and got the error. It has been frustrating.

Also I need to import from the above script to a unit test script and things are not working out !

from unitest_1 import rearrange_name import unittest class TestRearrange(unittest.TestCase): def test_basic(self): testcase = "Lovelace, Ada" expected = "Ada Lovelace" self.assertEqual(rearrange_name(testcase), expected) unittest.main()
Reply
#4
This part ([\w .]) only matches a single character.
It should be ([\w .]*) to match the full name part after the comma.
import re def rearrange_name(name): result = re.search(r"^([\w .]*), ([\w .]*)$", name) if result is None: return name return f"{result.group(2)} {result.group(1)}" name = "Lovelace, Ada" print(rearrange_name(name))
Output:
Ada Lovelace
import unittest from re_name import rearrange_name class TestRearrange(unittest.TestCase): def test_basic(self): testcase = "Lovelace, Ada" expected = "Ada Lovelace" self.assertEqual(rearrange_name(testcase), expected) if __name__ == '__main__': unittest.main()
Output:
λ python test_names.py Ada Lovelace . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
Also depend on the task this may be ok,or you may need to do to more testes.
As names also can contain apostrophes or hyphens.
testcase = "O'Neill, Eugene" expected = "Eugene O'Neill"
TheLummen likes this post
Reply
#5
Thank you ! It works.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 4,815 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
Question TypeError: argument of type 'NoneType' is not iterable Tajaldeen 7 6,117 Nov-29-2024, 09:45 AM
Last Post: Tajaldeen
  I am getting this TypeError: 'TreasureMap' object is not subscriptable. makilakos 2 2,098 May-25-2024, 07:58 PM
Last Post: deanhystad
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 4,247 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 2,848 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  TypeError: 'NoneType' object is not callable akbarza 4 19,655 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 8,390 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 5,119 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  TypeError: 'float' object is not callable #1 isdito2001 1 2,522 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 23,114 Jan-07-2023, 07:02 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.