0

I have a string array filled with smaller string arrays that I've split in to sets of three. It looks like this (except many more):

conv = ('http-get:*:audio/xxx', ':', 'YYY.ORG_XXXXXXXXXX;YYY.ORG_FLAGS=97570000000000000000000000000'), ('http-get:*:video/xxx', ':', 'YYY.ORG_PN=XXXXXXXXXXX;YYY.ORG_FLAGS=ED100000000000000000000000')

The only part of these arrays that I actually want is the third item in the list. How would I go about printing the third item only? My problem is that this is an array inside of an array.

3 Answers 3

1

Basically loop through the conv tuple and store/print out the 2nd object in each. It can be done as a traditional for-loop or using list-comprehensions. Try this -

>>> [i[2] for i in conv]
['YYY.ORG;YYY.ORG_FLAGS=97570000000', 'YYY.ORG_PN=XXXXXXXXXXX;YYY.ORG_FLAGS=ED100000']
Sign up to request clarification or add additional context in comments.

1 Comment

This worked! Thank you for giving me an elegant solution without getting too complicated
0

Like for so many other things, the solution is a list comprehension.

array = [[],[] etc. #your array]
print " ".join([item[2] for item in array])
>>>YYY.ORG;YYY.ORG_FLAGS=97570000000 YYY.ORG_PN=XXXXXXXXXXX;YYY.ORG_FLAGS=ED100000

Basically the key part here is the line:

[item[2] for item in array]

which iterates through array and returns the third value (zero-indexed) of each object it finds.

For loop equivalent:

result = []
for item in array:
    result.append(item[2])

Comments

0

you can try to build a tuple by extension

>>> conv3 = tuple(x[2] for x in conv)

and if you want to print them, you can for example use

>>> ', '.join(x[2] for x in conv)

Does it do what you wanted ?

5 Comments

Why would you build a tuple?
It might be useful if @PintSizeSlash3r only cares about the third items... and also to notice that syntax is the same for building the tuple and to print the third items only. It is an extra trick! For free!!
Maybe I was unclear, the same syntax works with any iterable, I'm just unclear as to how the tuple is related to the question, or why you chose a tuple. Did you see some benefit in an immutable type in this question? Is there some performance boost?
I put a tuple because @PintSizeSlash3r had a tuple. It was not a question of performance (which might be perhaps vacuous question for a tuple of length 2). Notice that there is no difference in performance between tuple and list as they are the same datastructure (but with different methods and properties). Anyway, in your answer there is no need to build the list and then print the items, this is a performance issue!
eh, reasonable. I printed out the list because OP asked how to print things out.

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.