0

I've been googling around for the answer to this question, but have had no luck to date.

Python lists.

I have a list with 3 rows of data in it.

K[0]
K[1]
K[2]

If for example I wish to go to a specific index number within the list discribed obove, say k[2] index 127, how do I do that. I want to be able to print the contents of that element at that index.

All help greatfully appreciated!

3 Answers 3

1

Do you simply mean:

print(K[2][127])

You can access sub-indices like you would any other.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you all very much! As it turns out my problem was not understanding the correct syntax to use. I kept getting unhashable errors no matter what I tried. Headache solved :-)
1

You can index into the container at K[2] exactly as you indexed into the container K:

element = K[2][127]

Example:

        # 0  1  2
>>> l = [[1, 2, 3], # l[0]
         [4, 5, 6], # l[1]
         [7, 8, 9]] # l[2]
>>> l[2][0]
7

1 Comment

Thank you all very much! As it turns out my problem was not understanding the correct syntax to use. I kept getting unhashable errors no matter what I tried. Headache solved :-)
1

I think you are describing a 2d array. To access single elements in a 2d array, simply use the double [] notation:

K[2][127] # index 127

1 Comment

Thank you all very much! As it turns out my problem was not understanding the correct syntax to use. I kept getting unhashable errors no matter what I tried. Headache solved :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.