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?

## All 6 Replies

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

ahhh was forgetting the : DOH!
Thanks

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

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))
``````

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.