0

I'm a beginner to python, but I can't find anything on the internet to help me with this problem:

I want to append different values to 2 different lists at once (the actual problem is more complex than this but I just need to know the right syntax so I'm simplifying it)

test1= []
test2= []
[test1.append(1) and test2.append(2) for i in range (10)]

this doesn't append to both lists, how do I make that happen?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • @j1-lee. There are a couple of alternatives that aren't so bad. – Mad Physicist Jan 27 '22 at 04:12
  • @MadPhysicist Oh I am afraid I removed my comment just before you wrote one. I removed it because I was afraid that I was generalizing my opinion too much. Thanks for your input! – j1-lee Jan 27 '22 at 04:14
  • 1
    @j1-lee. Your opinion was quite warranted in this case, and I agree with it entirely. When you are new, don't worry about fancy coding. If you notice, non of my solutions involve a comprehension. Well, maybe the generator and deque, but that's a horrible solution no one should ever use. – Mad Physicist Jan 27 '22 at 04:16
  • Your edit completely invalidates my answer by asking a different question. I recommend that you revert and ask another question. – Mad Physicist Jan 27 '22 at 04:17
  • @MadPhysicist I tried to ask a different question, but Stack Exchange said I can't ask another question today and that i should edit/ clarify my existing question. Sorry for any trouble that causes you. – cara_elliano123 Jan 27 '22 at 04:22
  • @cara_elliano123. I think you'd have to roll back to the original first and maybe select the answer. Alternatively, I can just post an answer – Mad Physicist Jan 27 '22 at 04:24
  • I rolled it back for you. See this question, it should be pretty close to what you want: https://stackoverflow.com/q/15956309/2988730. If yours is still distinct, ask another. Keep in mind that if you're using numpy, there shouldn't be any loops involved at all. – Mad Physicist Jan 27 '22 at 04:28
  • @MadPhysicist, thank you so so much!! – cara_elliano123 Jan 27 '22 at 05:00

2 Answers2

1

Firstly I don't get why you need it in 1 line, there seems to be no good reason for this, anyway I guess you could do this:

test1, test2 = [], []
for _ in range(10): test1.append(1); test2.append(2)
jamylak
  • 128,818
  • 30
  • 231
  • 230
0

If your specific example is what you want, use argument unpacking to assign multiple values at once:

test1, test2 = [1] * 10, [2] * 10

You can also use the zip(*x) idiom to transpose a 2-column list:

test1, test2, map(list, zip(*[[1, 2]] * 10))

If you're OK with having tuples instead of lists, you can omit the map(list, ...) wrapper.

The best way I know of having a comprehension with side-effects is using collections.deque with a size of zero, so it runs the iterator but does not store any results:

from collections import deque

test1, test2 = [], []
deque(((test1.append(1), test2.append(2)) for _ in range(10)), maxlen=0)

The generator creates tuples (None, None) every time (since list.append returns None), which the deque promptly discards.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • what i am really trying to do is something like this, but I want to check every 5000th point that is being created to append the average of all the points up till that point to another list, so I don't know where to put that (if i%5000==0 then do -----) in the line [points.append((np.random.uniform(0,1), np.random.uniform(0,1))) for i in range(100000)] – cara_elliano123 Jan 27 '22 at 04:09
  • @cara_elliano123. I strongly suggest you ask a question about that then. Your question is known as an XY problem: you ask about Y thinking it's going to help you solve X, when in reality you're just going off on an unrelated tangent. It's a fine line to know hoe much to share and how much to simplify, so kudos on making the jump to ask at all. – Mad Physicist Jan 27 '22 at 04:14
  • I'm sorry!! Like I said I'm new to python, and new to stack exchange. Thank you for taking the time to answer mw at all, I really appreciate it! – cara_elliano123 Jan 27 '22 at 04:19
  • @cara_elliano123. No worries, we were all new once. You can roll back your question in the edit history and ask another any time. If you feel inclined, you can select my answer if it answers the original question adequately – Mad Physicist Jan 27 '22 at 04:23