TypeError: list indices must be integers or slices, not str

We Are Going To Discuss About TypeError: list indices must be integers or slices, not str. So lets Start this Python Article.

TypeError: list indices must be integers or slices, not str

How to solve TypeError: list indices must be integers or slices, not str

First, array_length should be an integer and not a string:
array_length = len(array_dates)
Second, your for loop should be constructed using range:
for i in range(array_length): # Use `xrange` for python 2.
Third, i will increment automatically, so delete the following line:
i += 1
Note, one could also just zip the two lists given that they have the same length:
import csv dates = ['2020-01-01', '2020-01-02', '2020-01-03'] urls = ['www.abc.com', 'www.cnn.com', 'www.nbc.com'] csv_file_patch = '/path/to/filename.csv' with open(csv_file_patch, 'w') as fout: csv_file = csv.writer(fout, delimiter=';', lineterminator='\n') result_array = zip(dates, urls) csv_file.writerows(result_array)

TypeError: list indices must be integers or slices, not str

First, array_length should be an integer and not a string:
array_length = len(array_dates)
Second, your for loop should be constructed using range:
for i in range(array_length): # Use `xrange` for python 2.
Third, i will increment automatically, so delete the following line:
i += 1
Note, one could also just zip the two lists given that they have the same length:
import csv dates = ['2020-01-01', '2020-01-02', '2020-01-03'] urls = ['www.abc.com', 'www.cnn.com', 'www.nbc.com'] csv_file_patch = '/path/to/filename.csv' with open(csv_file_patch, 'w') as fout: csv_file = csv.writer(fout, delimiter=';', lineterminator='\n') result_array = zip(dates, urls) csv_file.writerows(result_array)

Solution 1

First, array_length should be an integer and not a string:

array_length = len(array_dates)

Second, your for loop should be constructed using range:

for i in range(array_length):  # Use `xrange` for python 2.

Third, i will increment automatically, so delete the following line:

i += 1

Note, one could also just zip the two lists given that they have the same length:

import csv

dates = ['2020-01-01', '2020-01-02', '2020-01-03']
urls = ['www.abc.com', 'www.cnn.com', 'www.nbc.com']

csv_file_patch = '/path/to/filename.csv'

with open(csv_file_patch, 'w') as fout:
    csv_file = csv.writer(fout, delimiter=';', lineterminator='\n')
    result_array = zip(dates, urls)
    csv_file.writerows(result_array)

Original Author Alexander Of This Content

Solution 2

Follow up on Abdeali Chandanwala answer above (couldn’t comment because rep<50) –

TL;DR: I was trying to iterate through a list of dictionaries incorrectly by focusing to iterate over the keys in the dictionary but instead had to iterate over the dictionaries themselves!


I came across the same error while having a structure like this:

{
   "Data":[
      {
         "RoomCode":"10",
         "Name":"Rohit",
         "Email":"[email protected]"
      },
      {
         "RoomCode":"20"
         "Name":"Karan",
         "Email":"[email protected]"
      }
   ]
}

And I was trying to append the names in a list like this-

Same error received

Fixed it by-

Fixed the error

Original Author rohetoric Of This Content

Solution 3

I had same error and the mistake was that I had added list and dictionary into the same list (object) and when was iterating over the list of dictionaries and hit a list type object then I would get this error since I was trying to access keys within each dictionary.

I had to made sure that I only added dictionary objects to that list

Original Author Abdeali Chandanwala Of This Content

Solution 4

In my case I was trying to change the value of a dict key but since my dict was there in a for loop and was getting changed to type list i was getting the same error.

for value in source_list:
    my_dict['my_key']=some_val
    dict=list(mydict)
    exctraction0 = dict[0]

i resolved it by making sure the type of dict remains the same by making a deepcopy and re-initializing after every iteration(that is what the use-case was all about).

copy_dict = copy.deepcopy(my_dict)
for value in source_list:
    my_dict =copy.deepcopy(copy_dict)
    my_dict['my_key']=some_val
    dict=list(mydict)
    exctraction0 = dict[0]

Original Author officialrahulmandal Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment