3
print(sgrades_flat) ['Barrett', 'Edan', '70', '45', '59', 'Bradshaw', 'Reagan', '96', '97', '88', 'Charlton', 'Caius', '73', '94', '80', 'Mayo', 'Tyrese', '88', '61', '36', 'Stern', 'Brenda', '90', '86', '45'] print(s_grades) ['F', 'A', 'B', 'D', 'C'] 

I want to combine sgrades_flat and s_grades to look like ...

['Barrett', 'Edan', '70', '45', '59', 'F', 'Bradshaw', 'Reagan', '96', '97', '88', 'A' 'Charlton', 'Caius', '73', '94', '80', 'B' 'Mayo', 'Tyrese', '88', '61', '36', 'D' 'Stern', 'Brenda', '90', '86', '45', 'C'] 

My current strategy is to use this code:

z=[] for i, x in zip(sgrades_flat[::5], s_grades): z.append(i+x) print(z) 

but that output is:

['BarrettF', 'BradshawA', 'CharltonB', 'MayoD', 'SternC'] 
4
  • 1
    The data structure should be fixed. Instead of having one long list, this should be a list of five lists [[list1], [list2], etc] Commented Jan 6, 2023 at 23:10
  • @MatBailie ok I've learned now how to create that grouped nested list, would the next step be to then iterate over that nested structure appending s_grades at the end of each element in the nest? and what does that look like.. Commented Jan 7, 2023 at 14:14
  • Assuming sgrades_flat is now a list of lists; z = [[*grades, s_grades[i]] for i, grades in enumerate(sgrades_flat)] Commented Jan 7, 2023 at 20:31
  • Or, z = [[*grades, new_grade] for grades, new_grade in zip(sgrades_flat, s_grades)] Commented Jan 7, 2023 at 20:36

3 Answers 3

3

I would combine the list by iterating manually on them:

sgrades_flat=['Barrett', 'Edan', '70', '45', '59', 'Bradshaw', 'Reagan', '96', '97', '88', 'Charlton', 'Caius', '73', '94', '80', 'Mayo', 'Tyrese', '88', '61', '36', 'Stern', 'Brenda', '90', '86', '45'] s_grades=['F', 'A', 'B', 'D', 'C'] it1 = iter(sgrades_flat) it2 = iter(s_grades) result = [] try: while True: for _ in range(5): result.append(next(it1)) result.append(next(it2)) except StopIteration: pass print(result) 

prints

['Barrett', 'Edan', '70', '45', '59', 'F', 'Bradshaw', 'Reagan', '96', '97', '88', 'A', 'Charlton', 'Caius', '73', '94', '80', 'B', 'Mayo', 'Tyrese', '88', '61', '36', 'D', 'Stern', 'Brenda', '90', '86', '45', 'C'] 

(this still looks like a bad idea as a flat list is sub-optimal for such a data structure)

Note that a one-liner without any manual iteration also does the same:

import itertools grouped_sgrades = list(itertools.chain.from_iterable( sgrades_flat[i:i+5]+[s_grades[i//5]] for i in range(0,len(sgrades_flat),5))) 

however, why flatten the lists?

grouped_sgrades = [sgrades_flat[i:i+5]+[s_grades[i//5]] for i in range(0,len(sgrades_flat),5)] 

result is a nice list of lists, which is approaching some structured data:

[['Barrett', 'Edan', '70', '45', '59', 'F'], ['Bradshaw', 'Reagan', '96', '97', '88', 'A'], ['Charlton', 'Caius', '73', '94', '80', 'B'], ['Mayo', 'Tyrese', '88', '61', '36', 'D'], ['Stern', 'Brenda', '90', '86', '45', 'C']] 
Sign up to request clarification or add additional context in comments.

5 Comments

[sgrades_flat[i*5:i*5+5]+[grade] for i, grade in enumerate(s_grades)]?
Or expand the slice out into a list with the letter grade. [[*sgrades_flat[i*5:i*5+5], grade] for i, grade in enumerate(s_grades)]
yes, that would work too
so if I had the non-flat version "sgrades=[['Barrett', 'Edan', '70', '45', '59'], ['Bradshaw', 'Reagan', '96', '97', '88'], ['Charlton', 'Caius', '73', '94', '80'], ['Mayo', 'Tyrese', '88', '61', '36'], ['Stern', 'Brenda', '90', '86', '45']]" I would just iter(sgrades) and append s_grades? would that append to each list as an iterable?
Not sure of what you're asking, but creating it = iter(sgrades) allows to get successive values of sgrades by using next(it) is a list comprehension.
1

You could use enumerate and then slice sgrades_flat accordingly:

sgrades_flat = [ 'Barrett', 'Edan', '70', '45', '59', 'Bradshaw', 'Reagan', '96', '97', '88', 'Charlton', 'Caius', '73', '94', '80', 'Mayo', 'Tyrese', '88', '61', '36', 'Stern', 'Brenda', '90', '86', '45'] s_grades = ['F', 'A', 'B', 'D', 'C'] z = [] for i, x in enumerate(s_grades): z += sgrades_flat[5*i:5*(i+1)] + [x] print(z) 

Comments

1

We need to iterate over both s_grades and sgrades_flat which we need to do 5 elements at a time. We can use a list comprehension with range based on 1/5th of the length of sgrades_flat. We can then use a second level of iteration in a list comprehension to bind j to that i * 5 index.

[ [*sgrades_flat[j:j+5], s_grades[i]] for i in range(len(sgrades_flat) // 5) for j in (i * 5,) ] # [['Barrett', 'Edan', '70', '45', '59', 'F'], # ['Bradshaw', 'Reagan', '96', '97', '88', 'A'], # ['Charlton', 'Caius', '73', '94', '80', 'B'], # ['Mayo', 'Tyrese', '88', '61', '36', 'D'], # ['Stern', 'Brenda', '90', '86', '45', 'C']] 

Alternatively, iterating over s_grades with enumerate:

[ [*sgrades_flat[j:j+5], g] for i, g in enumerate(s_grades) for j in (i * 5,) ] # [['Barrett', 'Edan', '70', '45', '59', 'F'], # ['Bradshaw', 'Reagan', '96', '97', '88', 'A'], # ['Charlton', 'Caius', '73', '94', '80', 'B'], # ['Mayo', 'Tyrese', '88', '61', '36', 'D'], # ['Stern', 'Brenda', '90', '86', '45', 'C']] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.