Reputation: 1289
I have 3 lists:
color = ['red','orange','purple','black']
number = ['2','4','6','77']
letter = ['K','R','U','Z']
what I want to end up with is:
mylist=[['K','R','U','Z','red','2'],['K','R','U','Z','orange','4'],['K','R','U','Z','purple','6'], ['K','R','U','Z','black','77']]
I tried:
for i in range(4):
letter.append(color[i])
letter.append(number[i])
This does not give me what I need.
Upvotes: 0
Views: 58
Reputation: 448
Not so beautiful but pretty simple in my opinion:
color = ['red', 'orange', 'purple', 'black']
number = ['2', '4', '6', '77']
letter = ['K', 'R', 'U', 'Z']
my_list = []
for i in range(len(color)):
new_list = letter[:]
my_list.append(new_list)
new_list.append(color[i])
new_list.append(number[i])
print(my_list)
Output:
[['K', 'R', 'U', 'Z', 'red', '2'], ['K', 'R', 'U', 'Z', 'orange', '4'], ['K', 'R', 'U', 'Z', 'purple', '6'], ['K', 'R', 'U', 'Z', 'black', '77']]
Upvotes: 0
Reputation: 3900
You can use a list comprehension for that :
color = ['red','orange','purple','black']
number = ['2','4','6','77']
letter = ['K','R','U','Z']
mylist = [ letter + [c, n] for c, n in zip(color, number) ]
print(mylist)
Upvotes: 1