0
var = ('3', [11, 13, 11, 11]) 

I want to convert it into something like this:

3 11 13 11 11 

so that I can pass it into another function

foo(*args): ... 
2
  • 3
    Frankly, the simplest would probably be foo(var[0], *var[1]) Commented Apr 16, 2021 at 6:08
  • 1
    @deceze Do not forget to convert var[0] to int, though. Commented Apr 16, 2021 at 6:15

2 Answers 2

1

You could use iterable unpacking to do this.

You can unpack an iterable by prefixing it with * when passing it into a function.

foo(int(var[0]), *var[1]) would give you what you want.

Sign up to request clarification or add additional context in comments.

Comments

0

i would suggest using a for loop to check if any item is an instance of list,tuple,dict,this is the code for the procedure:

var = ('3', [11, 13, 11, 11]) types=(list,dict,tuple) var=list(var) num=0 for x in var: item=var[num] num+=1 if isinstance(item,types): item2=item for x in item: var.append(x) continue var.remove(item2) continue 

here is an explanation of each line of code:

in types you store each possible type of container which could be found, this will be used when checking if the item is an instance of these classes. Next the variable is converted into a list as tuples are not mutable. num is the index(computers start counting from 0). The for loop just loops through each item and then the if block checks if the item is or is not an instance of the classes mentioned earlier in types variable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.