1

I am new to python and am trying to access a single specific element in a list of lists. I have tried:

line_list[2][0]

this one isn't right as its a tuple and the list only accepts integers.

line_list[(2, 0)]

line_list[2, 0]

This is probably really obvious but I just can't see it.

def rpd_truncate(map_ref):

    #Munipulate string in order to get the reference value
    with open (map_ref, "r") as reference:
        line_list = []
        for line in reference:
            word_list = []
            word_list.append(line[:-1].split("\t\t"))
            line_list.append(word_list)

    print line_list[2][0]

I get the exact same as if I used line_list[2]:

['Page_0', '0x00000000', '0x002DF8CD']
6
  • 3
    The first one is correct - why do you think it doesn't work? What happened when you tried it? Also post what line_list contains so we can help you. Commented Oct 11, 2013 at 10:27
  • you have to show your code. line_list[2][0] should work for list of lists, like [[1, 2, 3], [4, 5, 6]]. Are you sure you have list of lists? Are you sure you didn't get 'index out of range' exception? Commented Oct 11, 2013 at 10:29
  • @Brionius is correct. Multidimensional array access works this way. Try a = [4,5,6] ; b = [a,7,8]; print b[0][2] Commented Oct 11, 2013 at 10:31
  • this one isn't right as its a tuple and the list only accepts integers. A list can contain any Python object, unless you are using some sort of custom list. Reading elements from a tuple is the same as form a list. I wonder, are you trying to write new items to a list in this way? Show us your code and the error message you get. Commented Oct 11, 2013 at 10:35
  • First one will work fine on python lists and second, third will work fine on numpy arrays. Commented Oct 11, 2013 at 10:36

1 Answer 1

1

actually split will return a list more over you don't require word_list variable

for line in reference:       
    line_list.append(line[:-1].split("\t\t"))
print line_list[2][0]
Sign up to request clarification or add additional context in comments.

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.