-2

For example, num = [4, 6, 2, 5, 7]

for i in num:
  for j in num:
   j = num[i+1]

Is there a way to find if i is in the 0 position, 1 position, 2 position, ... so that I can make it were j = what position i is in +1

I also want to make it were if, lets say i was in position 1; if i == i+1: num.remove(i)

I already tried i+1 just doing i plus one and i could do a bunch of if statements and just make it do 1 over but i have like 4 variable for the for loop all in different position of i and I'm worried it will say list(x) out of index. I also tried .find put it didn't work for a list.

Also, I need to make it as too were it doesn’t change the original value of the integer so that I can add them up, would I have to make too lists.

1 Answers1

0

Yes, what you're looking for is the enumerate function, which takes in a list and gives you both the index and the value of each element in the list:

nums = [4, 6, 2, 5, 7]

for index, value in enumerate(nums):
  print(index, value)

# will print
# (0, 4)
# (1, 6)
# (2, 2)
# (3, 5)
# (4, 7)

Some extra info from RealPython: https://realpython.com/python-enumerate/

Bao Huynh Lam
  • 974
  • 4
  • 12