How do I concatenate each element of different lists together?

Surkles
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']
Jean-François Fabre

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I concatenate two lists together but with different frequencies

How do I concatenate two lists in Python?

How to compare each element of two different lists?

How to concatenate element-wise two lists of different sizes in Python?

How to concatenate two dictionary lists together in python?

I have two lists of strings in kotlin, how to concatenate each element of the first list with elements of the second one respective to the positions?

How do I concatenate tables with different properties?

How do I concatenate two lists inside nested list Python?

How do i add upp the values in list of list of lists together?

How do I store each element of a List in a Data Frame, when the lists elements are pages of text that need to be parsed?

With a hash of lists how do I operate on each key/list element once in random order?

Writing to a csvFile using two lists. How do i write each list to two different columns

How do I find the largest number in each position of three possibly different length lists and find the sum of them?

How do i target an inline element and a block level element together?

How to find the average of each element in two different lists

Haskell group elements in the list and concatenate different adjacent element together

How do I concatenate each line of 2 variables in bash?

How do i compare each element in an array with each element in List?

How to iterate two lists with different length together?

How do I concatenate my element id in this scenario?

How do I concatenate two arrays of different datatypes in BigQuery?

Unable to concatenate two lists together

How do I add strings together with different names

How do I create a torch diagonal matrices with different element in each batch?

Concatenate two lists of different length in between each other

How to compare each element of multiple lists and return name of lists which are different

Kotlin combining 'N' lists together by summing each element

How to concatenate all lists (each lists are made my for loop)

How do I concatenate cell values and text together using Excel VBA?