-3

I am trying to combine this code: subjects = ["physics", "calculus", "poetry", "history"] grades = [98, 97, 85, 88]

output needs to be [physics, 98],[calculus, 97] and so forth for the last two.

When I use zip to combine I get: <zip object at 0x7f71c6574c88>

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Alacrity
  • 1
  • 1
  • For reference, you can find the [documentation online](https://docs.python.org/3/library/functions.html#zip). It's worth knowing at least the built-in functions and skimming the list of modules, so you have some idea what already exists. (Obviously if you don't know the thing you want is called `zip`, just skimming the whole thing wouldn't help). – Useless Aug 05 '21 at 14:45
  • 1
    Oh, and `zip` returns a generator. That's why the answers given said `list(zip(...))` - that actually generates the list. – Useless Aug 05 '21 at 14:46
  • 1
    Post a minimal reproducible example of your problem – Kraego Aug 05 '21 at 14:53
  • @Alacrity please don't sabotage your question. The point of this website is to curate a resource for future visitors with the same problem as you, and if you throw out your question, there's no value or context left for the community. Thanks. If you want to filter a list, use a list comprehension: `[x for x in zip(subjects, grades) if x[0] != "poetry"]`. – ggorlen Aug 05 '21 at 15:23
  • sorry, did not know. I'm new to this whole thing I will make sure I don't do that again. Thank you for your help. – Alacrity Aug 05 '21 at 15:26
  • No problem. I recommend taking the [tour]. – ggorlen Aug 05 '21 at 15:28

4 Answers4

0

you can use zip

A=[1,2,3]
B=[4,5,6]
C=list(zip(A, B))
print(C)

gives:

[(1, 4), (2, 5), (3, 6)]

and in your case:

zip(subjects, grades)

will give the desired output

user107511
  • 772
  • 3
  • 23
  • How do I get rid of the brackets at the beginnning/end to make it cleaner? or is that how it' supposed to be? – Alacrity Aug 05 '21 at 14:46
  • the brackets are printed when you're printing a list. if you want to print without them you can iterate over the list and print the elements only – user107511 Aug 05 '21 at 14:47
  • or use: `",".join([str(x) for x in C])`, this gives you a string of the elements separated with commas – user107511 Aug 05 '21 at 14:48
0

You should use zip.

output = list(zip(subjects,grades)) 

should give you the output you want

C Haworth
  • 659
  • 3
  • 12
0

A few ways to do this, but I prefer just using zip()

    subjects = ["physics", "calculus", "poetry", "history"] 
    grades = [98, 97, 85, 88]
 
    zipped_list = list(zip(subjects, grades)) 

    print(zipped_list) 

RESULT: [('physics', 98), ('calculus', 97), ('poetry', 85), ('history', 88)]

Keep in mind that zip returns a list of tuples. Use list() on each element if you want a list of lists instead.

  • "Keep in mind that zip returns a list of tuples" -- in Python 3, `zip` [returns an iterator object](https://docs.python.org/3/library/functions.html#zip) which is what OP is asking about. `list` consumes the iterator. – ggorlen Aug 05 '21 at 14:57
  • "output needs to be [physics, 98],[calculus, 97] and so forth for the last two." It seemed to me that OP wanted a list of lists, should have clarified that I meant use list() in a for loop on each element. – clark-fischer Aug 05 '21 at 15:17
  • I might not have been clear. OP wrote: "When I use zip to combine I get: ``". That's the iterator, the return value of `zip`. As the [duplicate question](https://stackoverflow.com/questions/36974162/python-zip-two-lists) shows, the whole point of adding `list` to the code is that you're consuming the iterator returned by `zip` to produce a list (in most cases, you won't want to do this, consume the list in a loop). Python 2 does return a list of tuples. None of this is obvious from most of the answers in this thread. – ggorlen Aug 05 '21 at 15:20
  • 1
    Totally sorry, You are completely right. Thank you for the clarification! – clark-fischer Aug 05 '21 at 15:55
0

I am trying to combine this code:

subjects = ["physics", "calculus", "poetry", "history"]
grades = [98, 97, 85, 88]

output needs to be

Combining two lists doesn't have "output". It has a result. You get output when you either print the result, or in interactive use because the REPL happens to print it for you. It's important to say clearly what you mean.

 [physics, 98],[calculus, 97],...

When I use zip to combine I get: <zip object at 0x7f71c6574c88>

Yes, that's the result of combining the lists in the way you wanted. It's an iterator, which means you can iterate over it and it will yield first the tuple ('physics', 98), and then the tuple ('calculus', 97) and so on.

Here is the documentation for zip: link. It's worth reading the docs so you know what built-in functions exist.

This is why the existing answers actually said list(zip(...)). It iterates over each value to populate the list.

However, what you actually care about is the output, in a form which doesn't match the built-in formatting of a list of tuples. You want a string containing a comma-separated sequence of two-element lists.

Note that list is printed with [], and tuple is printed with (), by default. This is also described in the documentation, and it's also worth knowing your language's built-in types.

','.join(str(list(t)) for t in zip(subjects, grades))
Useless
  • 64,155
  • 6
  • 88
  • 132
  • Good answer, although I'd just let one of the many [clear dupes](https://stackoverflow.com/questions/36974162/python-zip-two-lists) handle it, and [`zip`](https://docs.python.org/3/library/functions.html#zip) returns an [iterator, not a generator](https://stackoverflow.com/a/2776865/6243352). Also, when using `join`, it's [better/faster](https://stackoverflow.com/questions/37782066/list-vs-generator-comprehension-speed-with-join-function) to to pass a list comprehension than a generator expression. – ggorlen Aug 05 '21 at 15:04
  • 1
    Yeah, my only concern about closing as a duplicate was that the OP didn't know to look for `zip`, and it doesn't help teach them how and why they _should_ know it. This answer is more about demonstrating how to ask better questions in future. – Useless Aug 05 '21 at 15:08