-1

The following cell creates three lists makers, revenues, and countries of the same length. Create two new list of tuples such that:

  • Each tuple takes the elements at the same index position from the three lists
  • The last element in the tuple is preceded by the string 'Made in '.

For example, the first and last tuples in the new list are ('Toyota', 265, 'Made in Japan') and ('Ford', 156, 'Made in US') respectively. Do not make any change to makers, revenues, and countries.

makers = ["Toyota", "Volkswagen", "Hyundai", "Daimler", "GM", "Honda", "Ford"]
revenues = [265, 260, 97, 185, 157, 138, 156]
countries = ["Japan", "Germany", "Korea", "UK", "US", "Japan", "US"]

My Code:

autos1 = [(makers, revenues, countries) for makers, revenues, countries in zip(makers, revenues, countries)]

Result:

[('Toyota', 265, 'Japan'),
 ('Volkswagen', 260, 'Germany'),
 ('Hyundai', 97, 'Korea'),
 ('Daimler', 185, 'UK'),
 ('GM', 157, 'US'),
 ('Honda', 138, 'Japan'),
 ('Ford', 156, 'US')]

Need help with adding the "Made In"

Chris
  • 26,361
  • 5
  • 21
  • 42
  • Does this answer your question? [How do I put a variable’s value inside a string (interpolate it into the string)?](https://stackoverflow.com/questions/2960772/how-do-i-put-a-variable-s-value-inside-a-string-interpolate-it-into-the-string) – mkrieger1 Jun 14 '23 at 21:41
  • @user2390182 Why? – Kelly Bundy Jun 14 '23 at 22:08
  • @KellyBundy Twas a joke. You never stop to learn: https://en.wikipedia.org/wiki/Daimler_Company Sounds like sacrilege tho – user2390182 Jun 14 '23 at 22:31

2 Answers2

0

You can add f-Strings. similar with str.format()

So your code will be like this:

autos1 = [(maker, revenue, f"Made in {country}") for maker, revenue, country in zip(makers, revenues, countries)]

Andrej Kesely also has the same answer.

Julia
  • 512
  • 6
0

You could apply mapping the f-string elsewhere: specifically in a generator expressin that gets zipped in with the other two lists.

>>> list(
...   zip(
...     makers,
...     revenues,
...     (f"Made in {c}" for c in countries)
...   )
... )
[('Toyota', 265, 'Made in Japan'), 
 ('Volkswagen', 260, 'Made in Germany'), 
 ('Hyundai', 97, 'Made in Korea'), 
 ('Daimler', 185, 'Made in UK'), 
 ('GM', 157, 'Made in US'), 
 ('Honda', 138, 'Made in Japan'), 
 ('Ford', 156, 'Made in US')]
Chris
  • 26,361
  • 5
  • 21
  • 42