Ok Guys, got a question on a nested list and how to return the values from it.

Writing a scout script for a game I play.

The return is this:
[[278,278,-1,[25076,u'name',3,6,u'',0,0,1,[0,0,None,5]]]]
[[278,279,-1,[25076,u'name',3,6,u'',0,0,1,[0,0,None,5]]]]

I'm wanting to place the coordinates i.e. 278 and 279 along with the 3rd value in this case -1 into a list.
In this format [278,278,-1][278,279,-1]

I can get the return of the first line but after that it just repeats the same line.

Any thoughts?

>>> data = [[278,278,-1,[25076,u'name',3,6,u'',0,0,1,[0,0,None,5]]]]
>>> data[0][:3]
[278, 278, -1]

One more twist to this question, the return values are not exactly what I thought they were. Thought it was a list but I was wrong. Returns look like this, with no comma between them:

Result from request =

[278, 278, -1, [25076, u'', 3, 6, u'', 0, 0, 1, 0, 0,0, u'', [0, 0, None, 5]]]
[278, 279, -1, [25077, u'', 3, 6, u'', 0, 0, 1, 0, 0,0, u'', [0, 0, None, 5]]]
[278, 280, -1, [25077, u'', 3, 6, u'', 0, 0, 1, 0, 0,0, u'', [0, 0, None, 5]]]

So when I iterate through the loop it errors or runs the same value.

When I use [0][:3] I get the error:
int object is unsubscriptable.

I am using:

for data in result:
    print data[0][3]

I'm sure I'm not doing something right

I figured it out, just needed to change
data[0][:3] to data[:3] worked like a charm

Edited 4 Years Ago by toyotajon93

Try this:

for data in result:
    print type(data)

If it is type string then do:

for data in result:
    # converts the string to list object
    data = eval(data)
    print data[0][:3]

Look at this:

data_str = "[278, 278, -1, [25076, u'', 3, 6, u'', 0, 0, 1, 0, 0,0, u'', [0, 0, None, 5]]]"

data_list = eval(data_str)

print(data_list)
print(type(data_list))

Edited 4 Years Ago by Lardmeister

If your data is read from a text file, your lines are strings even though they look like lists. Lardmeister's advice is good.

This article has been dead for over six months. Start a new discussion instead.