1
myDict = results(results)
print("Subscribers: " + myDict['items'][0]['statistics']['subscriberCount'])

I am trying to store results as a variable to then use to only display a certain part of the output.

If I write

results_print(results)

it prints correctly but I can't store it as a variable for some reason.

This is the content of the results:

{'pageInfo': {'resultsPerPage': 1, 'totalResults': 1}, 'items': [{'snippet': {'publishedAt': '2013-10-20T10:33:50.000Z', 'country': 'SE', 'localized': {'description': 'Trap Town, the best trap music out there.\n\nSubmissions: traptownsubmit@gmail.com', 'title': 'Trap Town'}, 'customUrl': 'traptownmusic', 'title': 'Trap Town', 'description': 'Trap Town, the best trap music out there.\n\nSubmissions: traptownsubmit@gmail.com', 'thumbnails': {'default': {'url': 'https://yt3.ggpht.com/-4E1iRrrd_c8/AAAAAAAAAAI/AAAAAAAAAAA/cJeykUlKy8s/s88-c-k-no-mo-rj-c0xffffff/photo.jpg'}, 'medium': {'url': 'https://yt3.ggpht.com/-4E1iRrrd_c8/AAAAAAAAAAI/AAAAAAAAAAA/cJeykUlKy8s/s240-c-k-no-mo-rj-c0xffffff/photo.jpg'}, 'high': {'url': 'https://yt3.ggpht.com/-4E1iRrrd_c8/AAAAAAAAAAI/AAAAAAAAAAA/cJeykUlKy8s/s240-c-k-no-mo-rj-c0xffffff/photo.jpg'}}}, 'statistics': {'videoCount': '354', 'commentCount': '0', 'hiddenSubscriberCount': False, 'viewCount': '1417504', 'subscriberCount': '12178'}, 'kind': 'youtube#channel', 'id': 'UCipITl9sF0qOhCyx9CyoPGA', 'etag': '"m2yskBQFythfE4irbTIeOgYYfBU/dy7xO_v59s1GVGj_ZJRXB_3rico"', 'contentDetails': {'relatedPlaylists': {'watchHistory': 'HL', 'uploads': 'UUipITl9sF0qOhCyx9CyoPGA', 'favorites': 'FLipITl9sF0qOhCyx9CyoPGA', 'watchLater': 'WL', 'likes': 'LLipITl9sF0qOhCyx9CyoPGA'}}}], 'kind': 'youtube#channelListResponse', 'etag': '"m2yskBQFythfE4irbTIeOgYYfBU/p-3fTLLWdWeBZO-Q-vqxfEbNIhw"'}

This is the function?

def channels_list_by_id(service, **kwargs):
    kwargs = remove_empty_kwargs(**kwargs)  # See full sample for function
    results = service.channels().list(
        **kwargs
    ).execute()

def print_results(results):
    print(results)
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223

1 Answers1

1

I guess you are trying to do this:

def get_subscriber_count(results):
    return results['items'][0]['statistics']['subscriberCount']

results = channels_list_by_id(service, **kwargs)
print("Subscribers:", get_subscribers_count(results))

If you are trying to assign an anonymous function to a variable as you can do in languages like Javascript, you can try this:

get_subscribers_count = lambda r: r['items'][0]['statistics']['subscriberCount']

But in Python this is limited to one-liners.

If results is a string, you need to parse it before being able to access keys:

import json

def get_subscriber_count(results):
    parsed_results = json.loads(results)
    return parsed_results['items'][0]['statistics']['subscriberCount']

# or
get_subscriber_count = lambda r: json.loads(r)['items'][0]['statistics']['subscriberCount']

[update]

One problem with channels_list_by_id it that it lacks a return statement, so it returns None. Unlike many other languages, Python does not return the value of the last statement, you must return explicitly:

def channels_list_by_id(service, **kwargs):
    kwargs = remove_empty_kwargs(**kwargs)  # See full sample for function
    return service.channels().list(
        **kwargs
    ).execute()
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153