-1

Hello guys i want to increas index for each object by +1, this is the code: how can i do it?

value = all_data[0].find_all("div", {"class":"th-value"})


for index in range(len(value)):
    mark = value[index].text
    model = value[index]
    year =value[index]
    fuel_type = value[index]
    engine = value[index]
    mileage =  value[index]
    cilinders =  value[index]
    transmission = value[index]
    drive_wheel = value[index]
    doors = value[index]
    wheel = value[index]
    color = value[index]
    interior_color = value[index]
    airbags = value[index]
    vincide =  value[index]
    interior_material = value[index]
Prometheus
  • 618
  • 1
  • 7
  • 23
gio
  • 1
  • 1
  • 1
    Do you mean finding the `index+1`th index? Or shift everything by 1 index position? – Prometheus May 23 '21 at 08:56
  • index +1 for each object. for example: mark = value[0] model = value[1] ................................ interior_material = value[15] – gio May 23 '21 at 08:59
  • Can you show us full code what are you trying to do so it will be better to understand – Bhavya Parikh May 23 '21 at 09:09
  • full code is to big to post. its parsing, value = all_data[0].find_all("div", {"class":"th-value"}) its a list and on every second index there is correct values for my variables – gio May 23 '21 at 09:14
  • how about you show `all_data` and what the expected output should be? if your full code is too big to post, you need to work on isolating your problem – Mulan May 23 '21 at 13:46
  • if all_data is a list you could add a dummy object at the beginning of it, like all_data.insert(0, None) . But this is a very uncommon way to work with list. You have to get used to index starting at 0 in Python lists – Malo May 23 '21 at 15:05

1 Answers1

1

Frankly I doubt it will help you, because I hardly can understand what you're trying to achieve and what a recursion has to do with your code. So here is just one trick, how you can make a simple 'incrementer' the variable (output of a function actually) that changes (gets bigger, for example) every time you call it:

def counter():
    c = {"count": -1}
    def num():
        c["count"] += 1
        return c["count"]
    return num

i = counter()

print(i()) # <-- 0
print(i()) # <-- 1
print(i()) # <-- 2
print(i()) # <-- 3 etc

Here you can read about nested functions: Why aren't python nested functions called closures?

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23