How to split, then append Array between 0 and Nth value, and then repeat that same step on the next set?

Viewed 51

I am trying to sort through my data, but it is coming back as one big array. I want to split the array from the 0 index to the 7th index, put it into its own array, and then do the same for the next 7, and then the next 7 until it goes through the whole array. Below is the code that I tried, but it did not work.

restaurant_list = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
                   '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
                   '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
                   '', '',]

def parse_restaurant_data(data):
    final_list = []
    for i in range(len(data[0:])):
        each_restaurant = data[0:7][i]
        final_list.append(each_restaurant)
    print(final_list)

parse_restaurant_data(restaurant_list)
4 Answers

Instead of each_restaurant = data[0:7][i], you should be dynamic and append list as per situation:

final_list = []
# demo list
restaurant_list = [
    'sknskn', 'snsnsn', 'kskn', 'ksnn', 'zazwi',
    'sknskn', 'snsnsn', 'kskn', 'ksnn', 'zazwi',
    'sknskn', 'snsnsn', 'kskn', 'ksnn', 'zazwi',
    'sknskn', 'snsnsn', 'kskn', 'ksnn', 'zazwi',
    'sknskn', 'snsnsn', 'kskn', 'ksnn', 'zazwi',
    'sknskn', 'snsnsn', 'kskn', 'ksnn', 'zazwi'
]

for i in range(0, len(restaurant_list), 7):
    final_list.append(restaurant_list[i:i+7])

print(final_list)

Try this-

restaurant_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

def parse_restaurant_data(data):
    final_list = []
    for i in range(0, len(data), 7):
        try:
            each_restaurant = data[i:i+7]
        except:
            each_restaurant = data[i:]
        final_list.append(each_restaurant)
    print(final_list)

parse_restaurant_data(restaurant_list)

Output-

[[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20]]

Try this approach that uses a list-comprehension:

def split_list(my_list, group_size):
    groups = range((len(my_list) + group_size - 1) // group_size)
    final = [ my_list[i * group_size:(i + 1) * group_size] for i in groups ]
    return final

Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Result: [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15]]

The nice part is that you can change the method signature from your original to support an arbitrary group size.

This is the way to do it:

restaurant_list = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',]

def parse_restaurant_data(data):
    final_list = []
    for i in range(len(data)):
        begin_index = i * 7
        stop_index = begin_index + 7
        restaurant = data[begin_index: stop_index]
        final_list.append(restaurant)
    print(final_list)

parse_restaurant_data(restaurant_list)
Related