Surkles
Surkles

Reputation: 65

How do I concatenate each element of different lists together?

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

Upvotes: 3

Views: 100

Answers (3)

Chris
Chris

Reputation: 36476

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

Upvotes: 1

Wulf
Wulf

Reputation: 11

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)

Upvotes: 1

Jean-François Fabre
Jean-François Fabre

Reputation: 140148

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

Upvotes: 3

Related Questions