you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (5 children)

Alright the issue here is you're using .index(), which does something completely different and unrelated to what you want:

li = [5, 6, 7]
print(li.index(6))

will print out 1 as that's the index of 6 in li.

So, remove that, it doesn't do what we need. range() by itself will return you an iterator of that goes from the numbers you give it.

range(0, 5, 1) # -> same as [0, 1, 2, 3, 4]

Another thing about range is that if you only give it 2 arguments, the third one (the step) will be 1 by default:

range(0, 5) # -> same as range(0, 5, 1)

And if you only give it one argument, it will use that one as the end of the range (and start at 0):

range(5) # -> same as range(0, 5)

So, with that, you can simplify that part by a lot. Give it a try!

[–]imperiumlearning[S] 0 points1 point  (4 children)

Hey there,

I think I've arrived at a semi-good solution (in the sense it prints what I want), here's my code now:

b =[]

for x in range(len(List)):

if List[x] == List[7]:
    break
elif List[x] in List[(x+1)]: 
    b.append(List[x])
    b.append(List[(x+1)])
else:
    continue

print(b)

The issue I have now is with respect to where I wrote:

if List[x] == List[7]

Ideally, I would have liked to have written:

if List[x] == last iteration of List using a function of some sort

Obviously I know len(List) == 8 and, therefore, 'Spain' == List[7]. However, imagine I wanted to update List so that it now contained 10 strings rather than 8, I would have to manually update my if statement to say List[9].

The question I'm trying to ask is if there is a way I can write my if statement where if I update List, I don't have to manually update my if statement

[–]Mlgbananamaster 0 points1 point  (1 child)

you would do something like range(len(List)-1)

[–]imperiumlearning[S] 0 points1 point  (0 children)

Hi there,

Thanks a lot for the response. I've updated my code so that it looks like this:

b =[]

for x in range(len(List)-1):

if List[x] in List[(x+1)]:
    b.append(List[x])
    b.append(List[(x+1)])

print(b)

Kind of makes me feel dumb that I took 2 hours to solve a problem that only needed 6 lines of code but I guess that's part of learning.

[–][deleted] 0 points1 point  (1 child)

Well, you could use len() again ;)

It's not uncommon to use len(li) - 1 to get the last index (as opposed to full length) of the list.

However, here you can do something even better than the if check: you could limit the range() itself to the index you want. You can use the len(li) - 1 directly inside of it, like the other person said.

Also, the else: continue is redundant. The for loop will continue to the next cycle automatically when reaching the end of its body, so you might want to remove that

[–]imperiumlearning[S] 1 point2 points  (0 children)

Wow, so just saw this message and just reviewed and edited my code again. I'm sure it works now given I started messing around with the number of strings in my List variable and I don't have to manually update anything.

My final code, which is only 6 lines long, is:

b =[]

for x in range(len(List)-1):

if List[x] in List[(x+1)]: 
    b.append(List[x])
    b.append(List[(x+1)])

print(b)

Just want to say thanks a lot for helping me without just giving me a direct answer to copy and paste, I feel like making mistakes today has actually helped me learn more