1

I am trying to create a single list with a tuple that contains integers

Examples of input:

test1((1, 2, 3), 2, 3)

test2((5, -5), 10, 3)

test3((10.2, -2.2, 0, 1.1, 0.5), 12.4, 3)

I have tried iterating through the tuple in various ways, but get a "int() is not iterable error". I tried to assign variables to inputs but get an "'type' object is not subscriptable" error.

I've rewritten the script many times now and don't have previous attempts saved. The unpack methods I've found on here don't work because they require me to iterate through the input, which I cannot do (or figure out how). b = [i for sub in args for i in sub], list(chain.from_iterable(args) ect haven't worked.

Here is what I have now

def checkio(*args):
    ab, cd, ef = args
    print (ab)
    empty = []
    empty.append(cd)
    empty.append(ef)
    for i in ab:
        empty.append(i)
    print (empty)

A bit of a mess. I'm quite new to python so I'm sure the solution is very simple

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Nick Kremer
  • 213
  • 1
  • 2
  • 6
  • 1
    All your inputs are regular; one tuple and two integers. Is that pattern *always* there? – Martijn Pieters Jan 06 '15 at 15:07
  • @Ashwini If the inputs are regular, like Martijn just asked, then this is not a duplicate – jamylak Jan 06 '15 at 15:08
  • @Martijn Pieters Let's say yes. I'm doing a mission on Chekio and that is all the input they are using to test my script, but I'm not sure if they will throw a curve ball at me. – Nick Kremer Jan 06 '15 at 15:09
  • @NickKremer: in that case just accept 3 arguments, and use `ab + (cd, ef)` to make a new tuple. It doesn't look like the first argument is ever anything other than a tuple with just more numbers. – Martijn Pieters Jan 06 '15 at 15:10
  • @jamylak I have looked through the site and haven't found anything that solves my problem. If I've overlooked a post, a link would be much appreciated – Nick Kremer Jan 06 '15 at 15:11
  • @NickKremer: but if you are getting the error you mentioned for the code you posted, then the first argument is *not* always a tuple. – Martijn Pieters Jan 06 '15 at 15:11
  • @NickKremer: your question has been closed as a duplicate. Reload the page if you don't yet see the link. – Martijn Pieters Jan 06 '15 at 15:12
  • I was under the impression that the code did work but OP thought it was messy – jamylak Jan 06 '15 at 15:13
  • @Martijn Pieters {ab + (cd, ef)} method worked. Thank you very much. Didn't expect an answer so quick. – Nick Kremer Jan 06 '15 at 15:15
  • @NickKremer: I removed your solution from the question; you could post that as a self-answer instead, but you also already marked my answer as accepted (offering the same solution). – Martijn Pieters Jan 06 '15 at 15:24
  • Ok. First post here on stackoverflow. apologies for any breach of normal protocol – Nick Kremer Jan 06 '15 at 15:25

3 Answers3

1

If your input is as regular as your samples, the answer is simple; create a new tuple by concatenating:

def checkio(ab, cd, ef):
    return ab + (cd, ef)

This concatenates a new tuple with (cd, ef) to the existing ab tuple.

For different patterns, you'll need to test the values; if all you ever get is tuples or integers, and the tuples are not nested, then that's as simple as:

def checkio(*args):
    result = []
    for value in args:
        if isinstante(value, tuple):
            result += value
        else:
            result.append(value)
    return tuple(result)

You can fight recursion with recursion; if the tuples can contain other tuples, flatten them with a recursive function:

def checkio(*args):
    result = []
    for value in args:
        if isinstante(value, tuple):
            result += checkio(*value)
        else:
            result.append(value)
    return tuple(result)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

If the pattern is always tuple which can be one of 2:

  • Tuple
  • Number

you can do the following more generic solution:

my_tuple = ((10.2, -2.2, 0, 1.1, 0.5), 12.4, 3)
my_list = []
for v in my_tuple:
    if isinstance(v, tuple):
        my_list.extend += v
    else:
        my_list.append(v)

print my_list

Output:

[10.2, -2.2, 0, 1.1, 0.5, 12.4, 3]

It's a question of pattern and how your data is struct.

Kobi K
  • 7,743
  • 6
  • 42
  • 86
  • Don't use a list comprehension for the side effects; you are creating a list object with the return values of `my_list.append()`, then discarding that list again. What a waste! Just use `my_list.extend(v)` or `my_list += v` here instead; these iterate for you. – Martijn Pieters Jan 06 '15 at 15:21
  • @Martijn Pieters you are right creating a `list` and not using it is a waste, I've fixed it with extend. – Kobi K Jan 06 '15 at 15:27
0

Have you tried this:

a = (1,2,3)
b = [*a]
print(b)

it converts the tuple to list. or simply print(*a)

Jason
  • 3,166
  • 3
  • 20
  • 37