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
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 410 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 524 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  TypeError: 'NoneType' object is not callable akbarza 4 1,020 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,389 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 1,519 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  TypeError: 'float' object is not callable #1 isdito2001 1 1,089 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 4,188 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  Help with python 'not subscriptable' error Extra 3 2,120 Dec-16-2022, 05:55 PM
Last Post: woooee
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,475 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 1,935 Oct-30-2022, 12:53 PM
Last Post: makeeley

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020