Conversation started Sep 16, 2019 at 15:21.
Sep 16, 2019 15:21
How to reverse list elements
@Aladdin In place? Or into a new list?
In place
Suppose you have a list [a,b,c,d,e]
What you do is start at the end and work towards the centre swapping elements as you go.
I did
So we use a for loop for i in range (0,len(mylist)/2)
That goes from the left end to the middle.
I would set a new variable j = len(mylist) - i - 1
Sep 16, 2019 15:29
Lst=[1, 2,3,4]
Then you'll swap the elements [i] and [j].
for I in range (lena(lst)):
Lst[I]==Lst[-i-1]
print (lst)
@JohnRennie but this prints the same list...
Well let's go through it by hand. len(lst) is 4 so your four loop will execute 4 times with i set to 0, 1, 2 and 3. OK so far?
the second line is lst[i] = lst[-i-1]. Is that correct? On the right hand side you have a negative number as the index i.e. -i-1.
Did you mean lst[i] = lst[len(lst)-i-1] ?
Sep 16, 2019 15:34
Yes
Also, did you really mean == ?
That's the comparison operator not the assignment operator.
I meant =
So let's just check this. Your code is:
lst=[1,2,3,4]
for i in range(0,len(lst)):
    lst[i] = lst[len(lst)-i-1]
print(lst)
Is that definitely correct?
Lst[I]=Lst [-i-1]
My code had this
That can't be correct because -i-1 is a negative number. You can't have a negative number as a list index.
We need to agree on what code you are running, then you need to try running the code we've agreed on to see what happens.
Ah, my bad, Python does allow negative indexes.
Ignore what I said
A negative index means you count from the right end of the list.
@Aladdin OK. So the code is:
lst=[1,2,3,4]
for i in range(0,len(lst)):
    lst[i] = lst[-i-1]
print(lst)
Yes?
Sep 16, 2019 15:49
Yed
But the problem is it's not printing backwards
I thought it could...
OK. Let's start with lst=[1,2,3,4]. On our first pass through the loop i=0 so the command in the loop does lst[0] = lst[-1]. Yes?
That changes lst to [4,2,3,4]. OK so far?
Yed
Ah I can see my mistake
This won't work
Yes :-)
You need an intermediate variable to do the swap.
Sep 16, 2019 15:53
So we use a for loop for i in range (0,len(mylist)/2)
That goes from the left end to the middle.
I would set a new variable j = len(mylist) - i - 1
Why did u take my list/2
Can u explain ur code
Because you are swapping the left and right halves of the list, so you only need to work your way through half the list.
Let's take your list [1,2,3,4] and see how this works. The first swap is with i=0 so j = 3. That swaps elements 0 and 3 so the list is now: [4,2,3,1]. OK so far?
What if length of list is odd
Ok
If the length is odd we don't process the middle element. But the middle element won't move anyway because it's in the middle.
So len(lst)//2 always works.
(I forgot we need // to do integer division)
My full code is:
lst = [1,2,3,4]

for i in range(0,len(lst)//2):
    j = len(lst) - i - 1
    x = lst[i]
    lst[i] = lst[j]
    lst[j] = x

print(lst)
Do you want to try running that on your PC?
Sep 16, 2019 16:02
Yes . On it
Yep it's working
Tbh I think these codes are more maths than coding
They are teaching you how to analyse a problem and construct an algorithm to solve it.
Sep 16, 2019 16:26
@JohnRennie hi
@Aladdin hi
I need to add elements of l1 and l2 into p
Such that no elements repeat
Replace c with p
@JohnRennie hello?
@Aladdin hi, sorry I was busy for a moment.
Oh okay. Ping me when u are back
I'm free now!
The way I would do this is to first put all elements of l1 into c.
Then I would step through l2, and I would append the elements of l2 to c only if that element isn't in l1.
Sep 16, 2019 16:34
Ah right
No need to make it complicated like mine
So that's my algorithm, and it now just remains to turn it into code.
Mine was
To first put those elements which are not Same in both lists
And then put those which are equal
Guess my algorithm is right?
I'm not sure how your algorithm works. Your description of it is a bit unclear to me.
First take a list then put those elements which are not in other list
Take the other list and put those elements which are not in other list
Finally take the elements which are common
@JohnRennie is this ok
Ah, I see. Yes that will work though it seems a bit inefficient because you are having to do lots of checking of whether elements are in lists or not.
Sep 16, 2019 16:42
Figured so. But just for practice I am gonna apply both mine and urs
OK, yes that's good practice.
@JohnRennie little help
@Aladdin yes?
For my code, how do I take the value of index only once for a list if the element it represents is not common
I can't use if statement.... It's taking i multiple times
One of the reasons people like Python is that it makes lots of things easy. In particular to check if a value is in a list you can just do if x in mylist:
So suppose you are getting all the elements of l1 that are not in l2. The way to do this is:
for i in range(len(l1)):
    if not l1[i] in l2:
        c.append(l1[i])
Do you see how that works?
Sep 16, 2019 16:56
Hoe does if not work
Is it similar to if?
Yes. The operator not just converts true to false and false to true.
So:
if 1 == 1:
    print("Equal!")
else:
    print("Not equal!")
That prints "Equal!" which I guess is obvious.
If you stick a not in:
if not 1 == 1:
    print("Equal!")
else:
    print("Not equal!")
Not equal
That will then print "Not equal!.
 
Conversation ended Sep 16, 2019 at 16:59.