[('BMW 3 SERIES ESTATE', 'Premium', 'GBP 1055.75', 'Automatic Transmission', '5', '5'), ('FIAT 500', 'Mini', '75.48', 'Manual Transmission', 'n/a', '3'), ('VAUXHALL CORSA', 'Economy', ' 79.48', 'Manual Transmission', '5', '5')]
[('14.00', '14.99', '39.00'), ('14.00', '14.99', '39.00'), ('14.00', '14.99', '39.00')]
I have these two zipped lists that I would like to join like this:
('BMW 3 SERIES ESTATE', 'Premium', 'GBP 1055.75', 'Automatic Transmission', '5', '5', '14.00', '14.99', '39.00')
How can I achieve this ? thx
CodePudding user response:
ls1 = [('BMW 3 SERIES ESTATE', 'Premium', 'GBP 1055.75', 'Automatic Transmission', '5', '5'), ('FIAT 500', 'Mini', '75.48', 'Manual Transmission', 'n/a', '3'), ('VAUXHALL CORSA', 'Economy', ' 79.48', 'Manual Transmission', '5', '5')]
ls2 = [('14.00', '14.99', '39.00'), ('14.00', '14.99', '39.00'), ('14.00', '14.99', '39.00')]
ans = ls1[0] ls2[0]
print(ans)
output:
['BMW 3 SERIES ESTATE', 'Premium', 'GBP 1055.75', 'Automatic Transmission', '5', '5', '14.00', '14.99', '39.00']
is this what you are trying to achieve?
CodePudding user response:
a = [('BMW 3 SERIES ESTATE', 'Premium', 'GBP 1055.75', 'Automatic Transmission', '5', '5'), ('FIAT 500', 'Mini', '75.48', 'Manual Transmission', 'n/a', '3'), ('VAUXHALL CORSA', 'Economy', ' 79.48', 'Manual Transmission', '5', '5')]
b = [('14.00', '14.99', '39.00'), ('14.00', '14.99', '39.00'), ('14.00', '14.99', '39.00')]
[x y for (x,y) in zip(a,b)]
Output
[('BMW 3 SERIES ESTATE',
'Premium',
'GBP 1055.75',
'Automatic Transmission',
'5',
'5',
'14.00',
'14.99',
'39.00'),
('FIAT 500',
'Mini',
'75.48',
'Manual Transmission',
'n/a',
'3',
'14.00',
'14.99',
'39.00'),
('VAUXHALL CORSA',
'Economy',
' 79.48',
'Manual Transmission',
'5',
'5',
'14.00',
'14.99',
'39.00')]
CodePudding user response:
Assuming you want to be able to add them all together, not just the first element:
data = [('BMW 3 SERIES ESTATE', 'Premium', 'GBP 1055.75', 'Automatic Transmission', '5', '5'), ('FIAT 500', 'Mini', '75.48', 'Manual Transmission', 'n/a', '3'), ('VAUXHALL CORSA', 'Economy', ' 79.48', 'Manual Transmission', '5', '5')]
nums = [('14.00', '14.99', '39.00'), ('14.00', '14.99', '39.00'), ('14.00', '14.99', '39.00')]
for i in range(len(data)):
data[i] = list(data[i])
for j in range(len(nums[i])):
data[i].append(nums[i][j])
CodePudding user response:
list_a = [('BMW 3 SERIES ESTATE', 'Premium', 'GBP 1055.75', 'Automatic Transmission', '5', '5'), ('FIAT 500', 'Mini', '75.48', 'Manual Transmission', 'n/a', '3'), ('VAUXHALL CORSA', 'Economy', ' 79.48', 'Manual Transmission', '5', '5')]
list_b = [('14.00', '14.99', '39.00'), ('14.00', '14.99', '39.00'), ('14.00', '14.99', '39.00')]
final_list = []
for element in list_a:
temp_list = []
k = 0
# First append each elements into a temporary list
for i in range(len(element)):
temp_list.append(element[i])
# Now append the items of list_b into temp_list
for j in range(len(list_b)):
temp_list.append(list_b[k][j])
final_list.append(tuple(temp_list)) # Appending to the final list after transforming into a tuple
k = k 1 # Incrementing in order to match the len of list_b
del temp_list # deleting the temp_list to avoid similar elements