1

I want to create a number of lists from a single list. The lists will either have 1 value or 3 based on if there is data from a user. However, since the data is attached to a specific user, the order must be maintained.

For example:

data = ['no data', 'choice 1', 'choice 4', 'choice 2', 'no data', 'choice 1', ...etc]

I want the output to be:

list1 = ['no data']
list2 = ['choice 1', 'choice4', 'choice 2']
list3 = ['no data']
list4 = ['choice 1'...]

How would I achieve this?

2 Answers 2

3

Unfortunately, itertools.groupby won't work if you need to chunk runs of non-'no-data' into groups of 3, or as singltons for each individual 'no-data'. So, here's a something else thrown together:

In [40]: def group_lists(data):
    ...:     final = []
    ...:     contains_data = False
    ...:     temp = []
    ...:     for sub in data:
    ...:         if sub == 'no data':
    ...:             if contains_data:
    ...:                 final.append(temp)
    ...:                 temp = []
    ...:                 final.append([sub])
    ...:                 contains_data = False
    ...:             else:
    ...:                 final.append([sub])
    ...:         else:
    ...:             contains_data = True
    ...:             if len(temp) < 3:
    ...:                 temp.append(sub)
    ...:             else:
    ...:                 final.append(temp)
    ...:                 temp = []
    ...:                 contains_data = False
    ...:     return final
    ...:

In [41]: data = ['choice 1', 'choice 2', 'choice 1', 'choice 1', 'choice 4', 'choice 1', 'no data']

In [42]: group_lists(data)
Out[42]: [['choice 1', 'choice 2', 'choice 1'], ['choice 4', 'choice 1'], ['no data']]

In [43]: data2 = ['choice 1', 'choice 2', 'choice 1', 'choice 1', 'choice 4', 'choice 1', 'choice1', 'no data']

In [44]: group_lists(data2)
Out[44]:
[['choice 1', 'choice 2', 'choice 1'],
 ['choice 4', 'choice 1', 'choice1'],
 ['no data']]

Original Answer

Use itertools.groupby:

In [15]: data = ['no data', 'choice 1', 'choice 4', 'choice 2', 'no data', 'choice 1']
    ...:

In [16]: import itertools

In [17]: grouped = [list(g) for _, g in itertools.groupby(data, lambda s: s == 'no data')]

In [18]: grouped[0]
Out[18]: ['no data']

In [19]: grouped[1]
Out[19]: ['choice 1', 'choice 4', 'choice 2']

In [20]: grouped[2]
Out[20]: ['no data']

In [21]: grouped[3]
Out[21]: ['choice 1']

Unpacking that rather unwieldy one-liner:

In [26]: lists = []
    ...: for _, g in itertools.groupby(data, lambda s: s == 'no data'):
    ...:     lists.append(list(g))
    ...:

In [27]: lists[0]
Out[27]: ['no data']

In [28]: lists[1]
Out[28]: ['choice 1', 'choice 4', 'choice 2']

In [29]: lists[2]
Out[29]: ['no data']

In [30]: lists[3]
Out[30]: ['choice 1']

So, the result is a lists of lists:

In [32]: lists
Out[32]: [['no data'], ['choice 1', 'choice 4', 'choice 2'], ['no data'], ['choice 1']]
Sign up to request clarification or add additional context in comments.

8 Comments

You solved the first part of my issue, thank you! However, the second part is that I want a maximum of 3 items per list, so what would I need to do in order to combine the solution you gave, with the ability to also divide correct input at every third item?
@fcb434 Sorry, I do not understand. What exactly about the solution does not work for you?
So in addition to creating lists when the data contains "no data", I also need to create a new list when a list contains 3 items.
@fcb434 That is precisely what I provided above.
If you change the code to data = ['choice 1', 'choice 2', 'choice 1', 'choice 1', 'choice 4', 'choice 1', 'no data'] I would need the output to be list1 = ['choice 1', 'choice 2', 'choice 1'], list2 = ['choice 1', 'choice 4', 'choice 1'], list3 = ['no data']
|
0

This seems to work and is fairly easy to understand. It doesn't create separate variables, but instead creates a list-of-lists named lists.

data = ['no data', 'choice 1', 'choice 4', 'choice 2', 'no data', 'choice 1', '...etc']

lists = []
tmp = []
for elem in data:
    if elem != 'no data':
        tmp.append(elem)
    else:
        if tmp:
            lists.append(tmp)
        lists.append([elem])
        tmp = []

if tmp:
    lists.append(tmp)

for i, sublist in enumerate(lists, start=1):
    print('list{}: {}'.format(i, sublist))

Output:

list1: ['no data']
list2: ['choice 1', 'choice 4', 'choice 2']
list3: ['no data']
list4: ['choice 1', '...etc']

Comments

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.