0

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

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by dmcadidas15
1

Is this what you are looking for?

list_1 = ['dogs', 'cats', 'pigs', 'hamsters', 'kids']
pos = list_1.index('kids')
print list_1[pos-3]
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.