In order to compare how environmentally friendly the energy is it depends how you measure the energy and what point you start from.
Starting point for measuring
Most people compare electric and petrol measuring from the point of charging / filling up. However you need to go back to the point of oil being extracted from the ground and electricity being generated.
Oil for petrol and Diesel
Once the oil is extracted it has to be transported, refined and then transported to the petrol station all of this consumes more energy.
Oil is normally transported by tankers across the ocean which are powered using Bunker fuel.
Electricity is generated from Coal; increasingly rare in the UK, Nuclear power or Renewables such as Wind or Solar power. As a consumer, assuming you have a home charger, most of your electricity will be from you home supplier. By choosing the supplier and tariff you can obtain a mix of Nuclear power and Renewables, or just Renewables.
Guide to Bunker fuel – Crown Oil