Home > Software design >  my list of 140 elements just print the first one
my list of 140 elements just print the first one

Time:10-18

My code prints just the first element of the list (activities), because of the 0; but, when I try [:] I get the 'list indices must be integers or slices, not str' type error.

I need to print the 140 elements inside my list but I really don't know how to fix it. And I don't really want to write another code because this is the only way that I could have the difference between two dates in seconds.

This is my code:

import json
import datetime
import copy


#Read json

with open('/Users/kenyacastellanos/Downloads/data.json') as json_data_file:
    data = json.load(json_data_file)
print(data)

#Just show first 10 elements
print("Sin ordenar:",data['activities'][:10])

## Realizamos el ordenamiento por llave, la llave es user_id, creamos una funcion lambda para el ordenamiento
data['activities'].sort(key = lambda x: x['user_id'])

#Just show first 10 elements
print("Ordenamos:", data['activities'][:10])

# Duration
date1 = datetime.datetime.fromisoformat(data['activities'][:]['answered_at'])
date2 = datetime.datetime.fromisoformat(data['activities'][:]['first_seen_at'])
difference_date = (date1-date2)
print("Duration in seconds:", difference_date.seconds, difference_date.microseconds)

This is what my list contains, 140 of these. enter image description here

"activities":[
      {
         "id":272961,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:09:53.830 00:00",
         "first_seen_at":"2021-09-19T21:09:40.830 00:00"
      },
      {
         "id":402616,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:10:27.830 00:00",
         "first_seen_at":"2021-09-19T21:10:09.830 00:00"
      },
      {
         "id":64828,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:13:19.830 00:00",
         "first_seen_at":"2021-09-19T21:13:03.830 00:00"
      },
      {
         "id":397256,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:18:06.830 00:00",
         "first_seen_at":"2021-09-19T21:17:47.830 00:00"
      },
      {
         "id":202872,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:19:00.830 00:00",
         "first_seen_at":"2021-09-19T21:18:54.830 00:00"
      },
      {
         "id":367396,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:23:30.830 00:00",
         "first_seen_at":"2021-09-19T21:23:18.830 00:00"
      },
      {
         "id":378117,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:25:12.830 00:00",
         "first_seen_at":"2021-09-19T21:25:05.830 00:00"
      },
      {
         "id":257362,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:30:30.830 00:00",
         "first_seen_at":"2021-09-19T21:30:11.830 00:00"
      },
      {
         "id":80097,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:34:23.830 00:00",
         "first_seen_at":"2021-09-19T21:34:13.830 00:00"
      },

CodePudding user response:

You're running into trouble because the data type returned by data['activities'][0] is very different from data['activities'][:].

data['activities'][0] returns the first dictionary in the list:

{
    "id":272961,
    "user_id":"izi57ti5",
    "answered_at":"2021-09-19T21:09:53.830 00:00",
    "first_seen_at":"2021-09-19T21:09:40.830 00:00"
}

On the other hand, any subset of the list, including data['activities'][:10] or data['activities'][:], returns a list:

>>> data['activities'][:]
[
      {
         "id":272961,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:09:53.830 00:00",
         "first_seen_at":"2021-09-19T21:09:40.830 00:00"
      },
      {
         "id":402616,
         "user_id":"izi57ti5",
         "answered_at":"2021-09-19T21:10:27.830 00:00",
         "first_seen_at":"2021-09-19T21:10:09.830 00:00"
      },
      ...
]

What you do with the result of this is to attempt to access the 'answered_at' item from the dictionary. In the first case, this works fine:

>>> data['activities'][0]['answered_at']
"2021-09-19T21:09:53.830 00:00"

But the second produces a TypeError, indicating that you can't access elements of a list using a string key, which is true.

Instead, you have to decide how you're going to modify your workflow to handle the many items within the list. For example, you could use a loop:

for entry in data['activities']:
    # entry now points to the dictionaries within the list
    
    date1 = datetime.datetime.fromisoformat(entry['answered_at'])
    date2 = datetime.datetime.fromisoformat(entry['first_seen_at'])
    difference_date = (date1-date2)

    # this will print out once for every element in data['activities']
    print(
        "Duration in seconds:",
        difference_date.seconds,
        difference_date.microseconds,
    )
  • Related