Reputation: 27
I have two lists as shown below,car_xy and car_id. I have wrote a sample code to merge the corresponding elements of both lists, but am not getting the desired output.
car_xy =[(650,700),(568,231),(789,123),(968,369)]
car_id =[284,12,466,89]
#required_details merges the two lists
required_details = list(set(car_xy+car_id))
#now if i do print required_details the ouput will be a list like;
required_details = [284,12,(650,700),89,(568,231),466,(968,369),(789,123)]
#the required details adds the information in list randomly. What if i want the first elements of both the list together, like
required_details = [[284,(650,700)],[12,(568,231),[466,(789,123)],[89,(968,369)]]
Any suggestions will be great.
Upvotes: 1
Views: 5030
Reputation: 990
you are searching for the zip
function. https://docs.python.org/3/library/functions.html#zip
car_xy =[(650,700),(568,231),(789,123),(968,369)]
car_id =[284,12,466,89]
required_details = zip(car_id,car_xy)
Edit: adding the following makes it a list, in case there is a need for mutation.
req_details_list = [list(pair) for pair in required_details]
Upvotes: 0
Reputation: 410
Assuming that the arrays are same length, you loop through them and combine them like this:
required_details = []
for i in range(len(car_xy)):
required_details.append([car_id[i], car_xy[i]])
Upvotes: 1
Reputation: 13356
Actually you need [list(pair) for pair in zip(car_id, car_xy)]
Upvotes: 7