0

I'm a beginner with Python and am really confused as to why this piece of code returns '0' - I'd be grateful for any input on why this may not be working.

myString = [["1","a"], 0, [""]
        
In: myString[int(myString[0][0])]
Out: 0
2
  • That code doesn't parse. Missing bracket? Commented Oct 10, 2021 at 11:45
  • Try evaluating parts of the expression until you find a specific one you don't understand. Commented Oct 10, 2021 at 11:45

2 Answers 2

4

You can trace your code like below:

>>> myString[0]
["1","a"]

>>> myString[0][0]
"1"

>>> int(myString[0][0])
1

>>> myString[1]
0
Sign up to request clarification or add additional context in comments.

Comments

2

the reason why is because myString[0][0] gives '1' and int('1') gives 1. myString[1] gives the element at position index 1, which is 0.

Comments

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.