fcb434
fcb434

Reputation: 83

Creating lists of variable length from one list

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?

Upvotes: 1

Views: 55

Answers (2)

martineau
martineau

Reputation: 123463

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']

Upvotes: 0

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95948

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']]

Upvotes: 3

Related Questions