Warning: session_start(): open(/tmp/sess_e032b6b1074063521641cc88995dad9e, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to solve TypeError: 'dict_items' object is not subscriptable in Python - LearnShareIT

How to solve TypeError: ‘dict_items’ object is not subscriptable in Python

TypeError: 'dict_items' object is not subscriptable (Python)

TypeError: ‘dict_items’ object is not subscriptable in Python is a common error related to the dict_items type in Python. The below explanations can help you know more about the causes and solutions to fix this error.

How does the TypeError: ‘dict_items’ object is not subscriptable in Python happen?

This error happens due to the following reason:

First, the error happens when you are attempting to index an element at a specific position in the dict_items. The dict_items is a type, this type is retrieved from the method items() of a dict type in Python. For example:

myDict = {'learnshareit': 'LearnShareIT', 'author': None}

# Get items in the dict
items = myDict.items()
print(items)

Output:

dict_items([('learnshareit', 'LearnShareIT'), ('author', None)])

The dict_items is a type in Python; it is different from the list type in Python as it is not subscriptable. Therefore, whenever you are trying to subscript the items in the dict_items type, you will encounter this error. For example:

myDict = {'learnshareit': 'LearnShareIT', 'author': None}
 
# Get items in the dict
items = myDict.items()
 
# Subscript an item in the dict_items type 
print(items[1])

Output: 

The example above shows that we have used the index syntax to single index the second item in the dict, but what we received is the error as above. It happens because in Python only these four types: dict, list, tuple or str can be subscriptable. If you want to index a list element or a list_items element, please read the following solutions.

How to solve the error?

Convert dict_items to a list

To solve TypeError: ‘dict_items’ object is not subscriptable in Python, which is because of the dict_items indexing in Python, you must use the list() function to convert it to a list to make it susscriptable:

myDict = {'learnshareit': 'LearnShareIT', 'author': None}
 
# Get items in the dict
myItems = list(myDict.items())
 
# Subscript an item in the list type 
print(myItems[0])

Output:

('learnshareit', 'LearnShareIT')

The items in the list is actually a tuple (a pair) and as it is a pair, it can be subscriptable too.

Using a for loop

Sometimes you don’t want to use the list() function to convert the dict_items because it will consume more memory for doing just a small task. In this case, you might want to iterate through each element in the dict_items directly through a for loop. 

myDict = {'learnshareit': 'LearnShareIT', 'author': None}
 
# Get items in the dict
items = myDict.items()
 
# Get items in dict_items type
for i in myDict.items():
    print (i)

Output: 

('learnshareit', 'LearnShareIT')
('author', None)

When you want to get the items at a given index in the dict_items, you can create a count variable and increment after each loop and print it out whenever the count matches the index you want.

myDict = {'learnshareit': 'LearnShareIT', 'author': None}
 
# Get items in the dict
items = myDict.items()
 
# Create a count variable
count = 0
 
# Get first item in dict_items type
for i in myDict.items():
    if (count == 0):
        print (i)
        # Break the loop whenver reach the desired position
        break
    else: 
        # Increment the count through each loop
        count = count+1

Output

('learnshareit', 'LearnShareIT')

As you can see, this approach doesn’t need a function to convert the dict_items to a list, but it uses a loop and goes from the first element to the last element until it reaches the desired index. Although this approach is thought to consume less memory, it will consume more time when the given index is far from the 0th position.

Summary

We have learned how to deal with the TypeError: ‘dict_items’ object is not subscriptable in Python. By using the first approach, as it is considered effective and quick, you can easily solve it.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *