1

I'm having trouble with my assignment. The requirement is

listA = ["a","b","c"]

listB = [1,2,3]

listC = [4,5,6]

I need to print out the list of tuples as in the following form:

[("a",(1,4)),("b",(2,5)),("c",(3,6))]

Here is what I've done:

a = ["a","b","c"]
b = [1,2,3]
c = [4,5,6]
bc = list(zip(b,c))
abc = list(zip(a,bc))
print(abc)

The output of my code is:

[('a', (1, 4)), ('b', (2, 5)), ('c', (3, 6))]

I'm not sure that my code is correct because it has a little difference between my output and the requirement's output. My output is 'a', 'b', 'c' instead of "a", "b", "c". Can anyone give me some idea, please?

Johny Dang
  • 19
  • 2
  • 6
    Signle quotes (') and double quotes (") are equivalent in Python. So your are fine, those are the same outputs – ktv6 Dec 22 '20 at 08:32
  • @ktv6 thank you so much. I thought single quote (') is for character and double quote (") is for string like C++ so it really made me confusing – Johny Dang Dec 22 '20 at 08:37
  • Does this answer your question? [Single quotes vs. double quotes in Python](https://stackoverflow.com/questions/56011/single-quotes-vs-double-quotes-in-python) – adir abargil Dec 22 '20 at 08:42
  • Thank you for your answer. After getting the the list, how can I print another list with second value is average of the 2 value in the first list. For example, after getting my list is [('a', (1, 4)), ('b', (2, 5)), ('c', (3, 6))], how can I write another function to print [('a',2.5),('b',3.5),('c',4.5)]? – Johny Dang Dec 22 '20 at 09:22
  • @JohnyDang, this is a different problem and you should post it as another question or edit it into your original post as the second question. – ktv6 Dec 22 '20 at 18:02

1 Answers1

0

Your way is correct, " or ' doesn't matter in python. But in Java ' is used for char, and " is used for String. I attached another approach.

container = []
for i in range(len(listA)):
         container.append((listA[i],(listB[i],listC[i])))
Ahmad hassan
  • 1,039
  • 7
  • 13
  • Thank you for your answer. After getting the container[], how can I print another list with second value is average of the 2 value in the first list. For example, after getting my list is [('a', (1, 4)), ('b', (2, 5)), ('c', (3, 6))], how can I write another function to print [('a',2.5),('b',3.5),('c',4.5)]? – Johny Dang Dec 22 '20 at 09:20
  • All are the same as up, just change (listB[i],listC[i]) to listB[i]+listC[i]/2 – Ahmad hassan Dec 22 '20 at 09:23
  • I meant just base on the final list, which is base on [('a', (1, 4)), ('b', (2, 5)), ('c', (3, 6))]. How can I do it without using the previous problem? – Johny Dang Dec 22 '20 at 09:29
  • average = sum(abc[i][1]) / len(abc[i][1]) <- you can use a for loop to access it and take the average, note that i is the index of the container and 1 is the index of the tuple – Ahmad hassan Dec 22 '20 at 10:10