N
N
niggaedition2018-09-26 07:56:28
Python
niggaedition, 2018-09-26 07:56:28

How to fix list indices must be integers or slices, not str error when getting username?

I'm trying to get the name using the users.get method, but for some reason it doesn't work.
here is the code:

userget = vk.users.get(user_ids = 1)
      name = userget["first_name"]
      print(name)

but the error comes out list indices must be integers or slices, not str
please help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2018-09-27
@niggaedition

userget = vk.users.get(user_ids=1)
      name = userget[0]["first_name"]
      print(name)

S
Sergey Gornostaev, 2018-09-26
@sergey-gornostaev

The function vk.users.get()returned you a list. List elements are accessed by index, not by key.

S
sgaynetdinov, 2018-09-26
@sgaynetdinov

Use the py-vkontakte library

>>> import vk
>>> api = vk.Api('YOUR_TOKEN')
>>> user = api.get_user('durov')
>>> user.first_name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question