'I have Zip function error while merging lists in to tuple in python

cars = ["Audi","BMW","Lamborghini","McLaren","Mercedes-Benz","Porsche","Rolls-Royce","Tesla","Volvo"]
models= ['R8','i8' ,'Aventedor', "P2", "AMG", "GT3 RS", "Ghost", "model S", "XC90"]
print zip(cars,models)

error: print zip(cars,models)
          ^
SyntaxError: invalid syntax

Legends! What is wrong with my code?



Solution 1:[1]

print() is a function as per Python Version 3. Here is the corrected code:

cars = ["Audi","BMW","Lamborghini","McLaren","Mercedes-Benz","Porsche","Rolls-Royce","Tesla","Volvo"]
models= ['R8','i8' ,'Aventedor', "P2", "AMG", "GT3 RS", "Ghost", "model S", "XC90"]
result = zip(cars,models)
print(list(result))

Output:

[('Audi', 'R8'), ('BMW', 'i8'), ('Lamborghini', 'Aventedor'),('McLaren', 'P2'), ('Mercedes-Benz', 'AMG'), ('Porsche', 'GT3 RS'),('Rolls-Royce', 'Ghost'), ('Tesla', 'model S'), ('Volvo', 'XC90')]

Solution 2:[2]

print(...) but you also need to make it a list before printing because zip creates a generator object

print(list(zip(cars,models)))

Solution 3:[3]

Assuming each list contains the same number of elements and that you're trying to print make and model together per line of output then:

cars = ["Audi","BMW","Lamborghini","McLaren","Mercedes-Benz","Porsche","Rolls-Royce","Tesla","Volvo"]
models= ['R8','i8' ,'Aventedor', "P2", "AMG", "GT3 RS", "Ghost", "model S", "XC90"]

for car, model in zip(cars, models):
    print(car, model)

Note the syntax for the print() function

Solution 4:[4]

cars = ["Audi","BMW","Lamborghini","McLaren","Mercedes-Benz","Porsche","Rolls-Royce","Tesla","Volvo"]
models= ['R8','i8' ,'Aventedor', "P2", "AMG", "GT3 RS", "Ghost", "model S", "XC90"]
    
tuple(zip(cars,models))

Perfect this code works!!! Thanks a lot guys ! Keep rocking

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eshaan Gupta
Solution 2 Rabinzel
Solution 3 Albert Winestein
Solution 4 ranga abi