1

I'm new to Python. Here I'm trying to access list object in python which is throwing following error.

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


print(a)

Output:

['{"id":9,"name":"Foo"}']

print(type(a))

Output:

<class 'list'>

print(a['id'])

Output:

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

print(a[0]['id'])

Output:

TypeError: string indices must be integers

print(a[0])

Output:

{"id":9,"name":"Foo"}

Expected Output

9
4
  • 1
    Expected output?
    – mhhabib
    Apr 17, 2021 at 8:14
  • 1
    a should be [{"id":9,"name":"Foo"}] if you want to access it as a dictionary. For accessing the id a[0]['id'] would be the right approach.Since you should first access the list element then choose the desired key value pair from dictionary. Apr 17, 2021 at 8:16
  • @MiladEsrafiliyan I tried but it giving me TypeError: string indices must be integers
    – Maqsud
    Apr 17, 2021 at 8:20
  • 1
    As I said the dictionary in the list should not be covered with '. Because python would mistake it as a string. Apr 17, 2021 at 8:24

4 Answers 4

3

First of all, you have taken the dictionary mistakenly with ' ' which indicates '{"id": 9, "name": "Foo"}' as a string. Remove quotation marks and iterate the list. Assume the list size is fixed to one dictionary list.

a = [{"id":9, "name":"Foo"}]

id = a[0]['id']
print(id)

Let's your list be long like

a = [{"id": 9, "name": "Foo"}, {"id": 10, "name": "Bar"},{"id": 11, "name": "FooBar"}]

Then get id by using loop

for l in a:
    print(l['id'])
7
  • I tried but it giving me TypeError: string indices must be integers
    – Maqsud
    Apr 17, 2021 at 8:20
  • Copy the above code and run it in your IDE. You're getting the error because you have taken a list of dictionary but expect a['id'] but in the list there is no element of id
    – mhhabib
    Apr 17, 2021 at 8:22
  • @Maqsud remove ' ' from the dictionary ['{"id":9, "name":"Foo"}'] to [{"id":9, "name":"Foo"}]
    – mhhabib
    Apr 17, 2021 at 8:26
  • 1
    '{"id":9, "name":"Foo"}' this a string and there is no scope to get id. For sure check print(type(a[0])) is str
    – mhhabib
    Apr 17, 2021 at 8:31
  • 1
    @Maqsud please go through the whole answer and comment. You can't get any output from a string by indexing any char/string value. If you need only 9 from a string then use a[0][7]. The index always must be an integer. As i said to get id from the list of dictionary you must be removed inverted comma from both end
    – mhhabib
    Apr 17, 2021 at 8:44
2

you are storing the dictionary as a string so the error you are getting is expected. If you want to access the id in the dictionary then store it as the dictionary itself. To access the id value you can check the below code

a=[{"id":9,"name":"Foo"}]
b=a[0]['id']
print(b)
1

list have not attribute allows key and value. then you can try with dictonary.

0

I was solve this by the following

  1. Passing [{"id":9,"name":"Foo"}] instead of ['{"id":9,"name":"Foo"}'] which can done by removing json_encode() while sending data to Flask Application. In my case was from Laravel / PHP.

  2. By accessing element using a[0]['id'] as pointed by @mhhabib

Not the answer you're looking for? Browse other questions tagged or ask your own question.