-2

imagine I have a tuple in which, among other things, a list is stored.

for example:

t = (1, 2, 3, 4 [5, 6, 7])

Well, I want all the numbers they are saved in tuple and list. How do I do that? In separate I know how to get the numbers they are saved in tuple. I would just unpack the tuple and to get all numbers, which is saved in lis, I would just iterate over it. But in this situation I don't know what I have to do.

Any ideas?

For more details

def work_with_tuple(my_tuple = None):

    count_items = len(my_tuple)

    if count_items == 2:

        first_item, second_item = my_tuple

        print "first_item", first_item
        print "second_item", second_item

    if count_items == 3:

        first_item, second_item, third_item = my_tuple

        print "first_item", first_item
        print "second_item", second_item

        # But I have to know, that 'third_item' is a list.
        # Should I check every unpacked item if it is a list?
        # I think my idea it doesn't sound elegance.
        one, two = third_item
        print "one", one
        print "two", two

print "run first"
print "---------"
#   I call the function with easy tuple
work_with_tuple(my_tuple = (2, 3))
#   output: first_item 2
#           second_item 3
print ""
print "run second"
print "----------"
#   I call the function with nested tuple
work_with_tuple(my_tuple = (2, 3, [4, 6]))
#   output: first_item 2
#   second_item 3
#   one 4
#   two 6
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sophus
  • 379
  • 6
  • 21
  • 1
    Is the output you're expecting `[1, 2, 3, 4, 5, 6, 7]` ? – Jon Clements Apr 24 '18 at 16:16
  • To access the list in the tuple, use `t[-1]`. – Aran-Fey Apr 24 '18 at 16:19
  • @JonClements; I rather expect the output more like this: 1, 2, 3, 4, 5, 6, 7 - no a new list. – Sophus Apr 24 '18 at 16:19
  • @Aran-Fey: What does [-1] say? – Sophus Apr 24 '18 at 16:20
  • So - I'm taking a guess you actually want a string of comma delimited elements then? – Jon Clements Apr 24 '18 at 16:20
  • @Aran-Fey I'm not sure removing the python2.7 tag is appropriate here – Jon Clements Apr 24 '18 at 16:21
  • @Sophus, you did not understand Jon Clements' question. Do you want to return a FLATTENED list? – Mr. Polywhirl Apr 24 '18 at 16:21
  • What is `1, 2, 3, 4, 5, 6, 7` then? A string? An iteration? – tdelaney Apr 24 '18 at 16:22
  • Is the data just tuples lists and numbers or could it be other things like `(1, 2, 3, "foo")` (and what should that look like?) – tdelaney Apr 24 '18 at 16:23
  • @JonClements Why? Version tags aren't supposed to signalize which python version the OP is using. No part of this question is specifically about python 2. – Aran-Fey Apr 24 '18 at 16:25
  • Guys, It just a example. In general I work with tuple - no list in there. Sometimes there is possible that a list is in tuple. I just want to access to the elements in tuple and I also need the elements in the list. – Sophus Apr 24 '18 at 16:26
  • It may help to have some code demonstrating what you want. You say you don't want a list but show a list of integers. I don't know what you want. – tdelaney Apr 24 '18 at 16:29
  • 5
    @Aran-Fey mostly because there's answers that are completely easy in 3.x with extended unpacking and or new methods in libraries that 2.x just doesn't have - if the OP is definitely on 2.7 - then it's a waste of time for people recommending duplicates or answers that the OP can't possibly use. A 2.7 tag along with the main tag is mostly useful in these cases as a constraint as to what's possible or workable for the OP. – Jon Clements Apr 24 '18 at 16:33
  • @JonClements I think we should post those answers anyway, even if the OP can't use them. StackOverflow isn't all about the OP. Posting more answers is better for all the folks who come here from google and aren't using python 2. The OP can still pick an answer that works in python 2. – Aran-Fey Apr 24 '18 at 16:42
  • I updated my question. – Sophus Apr 24 '18 at 16:59
  • 2
    @Aran-Fey: don't remove the tag please. The OP is clearly using Python 2, the tag helps communicate that, people with a similar problem *on Python 2* can find this post with that tag. It's appropriate here. If you want to answer with a Py3 solution, so be it, but in this specific case I'd expect a helpful answer to address Python 2 first, and perhaps as a bonus extra Python 3. – Martijn Pieters Apr 24 '18 at 17:17
  • @MartijnPieters I don't understand. The tag description even says _"Do not use this tag simply to convey the version of Python you're using, unless the question concerns an issue specific to Python 2.7"_. Given that, how can we justify the use of the `python-2.7` tag on this question? If we want the question to be discoverable for python 2 users, wouldn't it be sufficient/better to edit a statement like "I'm using python 2" into the question body or title? And if you're right and this is an appropriate use of the tag, am I supposed to *add* the `python-x` tag to questions that don't have it? – Aran-Fey Apr 24 '18 at 18:00
  • 1
    @Aran-Fey: This is getting way too meta for this pure question. Can we either move this to the Python chatroom or to Meta? – Martijn Pieters Apr 24 '18 at 18:45

2 Answers2

0

You can iterate over the tuple and check each item.

def unpack(t):
    numbers = []

    for item in t:
        if isinstance(item, list):
            numbers += item # add each item of the list into accumulator
        else:
            numbers.append(item)

    return numbers

If you need something more generic, see this answer.

Gabriel
  • 1,922
  • 2
  • 19
  • 37
0

This way to access the list in the tuple.

tl = (1,2,3,4,[5,6,7])
print tl
for i in tl:
    if isinstance(i, list):
        print i[2]
    else:
        print i

I hope your problem is solving using my code.

Umesh
  • 1
  • 1