Im trying to access a variable in a list that is 3 places earlier in the list. For example list_1[dogs, cats, pigs, hamsters, kids]. If Im trying to access the word cats based on the position of kids how would I do that? I know list_1[4] returns kids and I want to be able to return cats without using list_1[1]. I tried making list_1[4] = a and then doing print list_1[a - 3] but i get a syntax error since I cant subtract an int from a string. Anyone think they can help me out? Thanks

Recommended Answers

All 2 Replies

Is this what you are looking for?

list_1 = ['dogs', 'cats', 'pigs', 'hamsters', 'kids']
pos = list_1.index('kids')
print list_1[pos-3]

Yes! Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.