John John - 15 days ago 7
Python Question

zip function is returning duplicates

Currently my list is:

[[(881, 886), ('Twilight Sparkle', 'Rainbow Dash')],
[(883, 885), ('Applejack', 'Rarity')],

[(887, 884), ('Princess Celestia', 'Pinkie Pie')],

[(888, 882), ('Princess Luna', 'Fluttershy')]]


I want it instead to have the format:

[(pid1, pname1, pid2, pname2),

(pid3, pname3, pid4, pname4),

(pid5, pname5, pid6, pname6),

(pid7, pname7, pid8, pname8)]


So that it is player1, name 1, player2, name 2

Is it better to do it that in the previous zip function and zip it a different way or manipulate the list now.

I thought to do it

temp = x[1]
x[1] = x[2]
x[2] = x[3]


But I'm not sure how to do it.

This is the zip function:

for i in range(0,length, 2):
pairs =zip(list[i],list[i+1])


Prior to zipping it was in this format:

[(881, 'Twilight Sparkle'), (886, 'Rainbow Dash'),
(883, 'Applejack'), (885, 'Rarity'),
(887, 'Princess Celestia'), (884, 'Pinkie Pie'),
(888, 'Princess Luna'), (882, 'Fluttershy')]


Is there a way to change the zip function so that it puts the first two in a list, then the second two, and so on but maintains the order of id, name, id, name rather than combining the id and name?

And I want it to be in the format:

[(881, 'Twilight Sparkle',886, 'Rainbow Dash'),
(883, 'Applejack',885, 'Rarity'),
(887, 'Princess Celestia', 884, 'Pinkie Pie'),
(888, 'Princess Luna', 882, 'Fluttershy')]

Answer

Looks like you don't want to zip at all, you just want to concatenate pairs of tuples:

players = [(881, 'Twilight Sparkle'), (886, 'Rainbow Dash'),
           (883, 'Applejack'), (885, 'Rarity'),
           (887, 'Princess Celestia'), (884, 'Pinkie Pie'),
           (888, 'Princess Luna'), (882, 'Fluttershy')]
pairs = [players[i] + players[i + 1] for i in range(0, len(players), 2)]
for pair in pairs:
    print(pair)

Output:

(881, 'Twilight Sparkle', 886, 'Rainbow Dash')
(883, 'Applejack', 885, 'Rarity')
(887, 'Princess Celestia', 884, 'Pinkie Pie')
(888, 'Princess Luna', 882, 'Fluttershy')
Comments