0

[enter image description here][1]I have a dataset that is a list of 3 nested lists (called "patients"). Each nested list contains a certain amount of observations. The first nested list contains 119 observations, the second one 9 and the third list contains 6 observations. These observations are dictionaries containing some keys such as "start time" and "end time".

Now I want to create a for loop to create a new list with 3 nested lists. I want each new nested list to only include the start times of that specific patient. So, 1 list that contains 3 lists with different sizes (119, 9 and 6)

However, when I try this, I get a list with 3 nested lists that all have the same size, all the nested lists include all 134 observations (119+9+6, called "start_times") . Is there somebody that can please help me with this?

`

start_times = [[] for i in range(3)]

for a in patients:
    for b in a:
        for c in range(3):
            start_times[c].append(b['start'])

` [1]: https://i.sstatic.net/0us5J.png [2]: https://i.sstatic.net/yHEIl.png

3
  • Welcome to Stack Overflow! In the future, please make sure input and output (both expected and actual) are included in text form in the question. This makes it easier for people to help you, because they won't have to guess at what's happening. Commented Dec 14, 2022 at 16:03
  • @Jasmijn Thank you! Do you mean instead of providing pictures? Because my input is "patients" but this is too big to provide in text form.. Commented Dec 14, 2022 at 16:12
  • Yes, instead of image form. It's usually better (both for reasons of privacy and input size) to provide fake data instead, something that is representative of the kind of data you're actually working with. Commented Dec 14, 2022 at 16:16

2 Answers 2

0

You'll want to use a nested list comprehension for this. I've changed the variable names for clarity, and added white space to make the structure clear:

start_times = [
    [
        observation['start']
        for observation
        in patient
    ]
    for patient
    in patients
]
Sign up to request clarification or add additional context in comments.

2 Comments

The answer of Jasmijn worked for me. However, I want to do the exact same for the key "timeserieslabel" in the dictionary. This key is not a string like the keys "start" and "end", but it is another list. For example the list ['Other artefacts']. When I click on that, I get a string called " Other artefacts". So, I would like to have a list called "labels" that contains 3 nested lists. I want these nested lists to contain the strings, just like the "start_time" vector. When I use Jasmijn's approach for this, the nested lists contain new lists instead of strings. This requires an extra step
I suggest writing a new question for this
0

You probably want to do this instead:

start_times = [[] for i in range(3)]
for idx, sublist in enumerate(patients):
    for record in sublist:
        start_times[idx].append(record["start"])

1 Comment

[] * 3 == [] and therefore [[] * 3] == [[]], so this does not work as intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.