Can't really deal with lists, help me please


Everytime that i get some exercise about interacting with the list, i get a lot of doubts. For example:

I have a list of numbers, and i want i get each of them and multiply by the others inside the list.

In 13/15 "product" they ask me for multiplying the numbers in the list, like [1,2,3,4] get me 1*2*3*4.

But there is all my questions and doubts that reach me every exercise with lists. How do I interact with Single items inside the list, and at the same time interact with another in the list? Because i build something like "for item in lst: do something"; But this method from where i understand, if inflicts one item by one.

To make a little bit clear, I don't understand how to create something like:

for item in mylist, interact with another item inside the list, by product or sum or subtract, And get me the result._

If somebody could help me understanding this, because all the time i get soemthing to deal with items in the list interactcing with another in the list i get really confused.

Joao Vitor


Hello, @moscrow!
So according to your quote you're supposed to come up with a program who does some sort of calculation with other items inside a list while iterating over the same? Interesting!
In this case, I'd suggest you to make a copy of the original

oldList = newList[:]

And work on it.

Q: But why make a copy?
A: That's because your program might say you'll need to calculate with values from your original list, which weren't modified. Oh well, that's coding!

If you remember, lists can be accessed by their index so using a range(len(listName)) will bring you a list containing elements from 0 to Lenght - 1 and such can be used on for loop to go from position to position or to go from position zero to position two (in this case, you'll need to use arithmetics - add, sub, mult, div -)
You can also create variables outside your loop instruction that are set with specific values so you can access specific positions of it.

Hope my answer helps you, I have to say it was abit complicated to understand your question.
If my answer wasn't what you were expecting, let me know and I'll try to find another way to explain.


Well, this cleared my mind a little bit, thanks.

Adding this "[:]" command creates a copy to the other variable? But if i want to copy the list like this:

oldList = newList[ : ]

wouldn't be the same as:

"newList = oldList" ?
and when i print, it will be the same as using [ : ]?

Because if i use [ : ] will get me the same thing:

oldList = [1,2,3]
oldList = newList[ : ]
print newList
console goes for: [1,2,3] right?

The point for my question now, it's why do I use newList[ : ] to get a copy, if i use newList = oldList would give me the same results?

And thanks for helping me, i really appreciate that!


Not really. Lists are stored by reference. So, when you do newList = oldList, newList doesn't hold a copy (which will have a new reference) of the oldList, but instead a reference to the same oldList

And that is why you can see unexpected behavior like:

old = [1, 2, 3]
new = old
old[1] = 10
print old
print new # oops, new got modified

old = [1, 2, 3]
new = old[:]
old[1] = 10
print old
print new # we are safe :D

EDIT: Looks like @g4be posted at the same time as me :stuck_out_tongue:


@moscrow: I think I've answered this somewhere but can't seem to find my post but I'll explain!
Everything in Python is pretty much a reference and a reference is something that lets you do things with the original "thing" that's holding the values.
Think this way: imagine if you had the password of someone's e-mail. With that password, you could access that person's e-mail and do anything you'd like to do.
That's how references work! You pass the reference to something and you'll be able to modify anything, in this case lists and following the instructions regarding list manipulation, but you'll lose your original list and some cases you really don't wanna do that.

In this case, you'd just like to get a copy of the list, not the reference of it.

1 - copy: you get the values from a list and you'll be able to modify those 
values without modyfing the original list
2 - reference: you get the reference of your original list and any modification
 made by your code using the argument/variable holding the reference, 
will modify the original list

I think the @gaurangtandon + g4be combo will put an end on your doubts :v:


woooow @g4be @gaurangtandon NICE! now i've got it. Two great answers at the same time, i'm lucky haha

Thanks for helping me guys!! :grinning:


No problem, Joao! Glad to help!