-2
variable1 = 1
variable2 = 2
a_tuple = (variable1, variable2)

def my_function(a):
    pass

my_function(a_tuple(variable1))

Is there a way I can pass a specific value from a tuple into a function? This is a terrible example, but all I need to know is if I can pass variable1 from the tuple into the function, I understand in this instance I could just pass in variable 1, but its for more complicated functions that will get its data from a tuple, and I don't like the look of that many variables, too messy.

variable1 = 1
variable2 = 2
a_tuple = (variable1, variable2)

def my_function(a):
    pass

my_function(*a_tuple)

This code would obviously provide an error as it unpacks the tuple and inserts 2 variables, to make this work in my program I would need a way to pass either variable1 or variable2 into the function. My question is can I define exactly which items from a tuple are passed into the function when calling the function? Latest version of Python if it matters.

P.S. I wrote print("hello world") for the first time 7 days ago, this is my first language and my first question I couldn't find an answer to. Go easy on me, and thank you for your time.

Grave
  • 68
  • 6
  • 3
    `my_function(a_tuple[0])` - or did I misunderstand your question? Also, your 'tuple' is actually a list. – Thierry Lathuille Aug 03 '20 at 09:05
  • 3
    Please strongly consider to do a [basic Python tutorial](https://docs.python.org/3/tutorial/index.html). Indexing is a fundamental operation of the language. – MisterMiyagi Aug 03 '20 at 09:11
  • 1
    Are you absolutely sure that you've read some tutorial and/or this Python page in particular: https://docs.python.org/3/tutorial/datastructures.html ? – tevemadar Aug 03 '20 at 09:12

3 Answers3

0

In the code you provided you don't have a tuple you have a list. But it is still pretty much the same.

In your example lets say that you wanted to pass the first variable you would do it like this:

my_function(a_tuple[0])

If you don't understand why there is a zero here and how does this work I highly suggest learning about lists before functions.

Marko Borković
  • 1,884
  • 1
  • 7
  • 22
  • Thank you for your guiding answer. So to get ranges from multiple tuples into a function i have to do something like this, 'def my_function(a, b, c, d):' which requires 4 values, and 'my_function(*a_tuple[0:2],*b_tuple[0:2])' which i just tested and works, Thanks again – Grave Aug 03 '20 at 09:33
0

You just need to access individual elements of the tuple, using index notation:

my_function(a_tuple[0])

or

my_function(a_tuple[1])

You could, if you wanted, write a new function which takes a tuple and an index, and calls my_function with the appropriate element:

def my_other_function(tuple, index):
    return my_function(tuple[index])

But I don't see how there would be much gain in doing that.

Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34
-1

you can index a tuple or use the index method.

    def my_function(a):
        pass

    my_function(a_tuple[0])

if you want to get the index of a value use the index() method

    a_tuple.index(variable1) #this will return 0
Sebastian Baum
  • 365
  • 1
  • 9
  • 3
    What's the point of suggesting ``tup[tup.index(val)]``? Why not just use the value directly? – MisterMiyagi Aug 03 '20 at 09:12
  • ofc he can use the index directly or pass the value directly. I wanted to make another solution to show him how to get an index or value by the value itself. I know it would be much easier to just pass the index or the value, but I don't know what he is going to do with the Code. You are right that a index or value is much more neater – Sebastian Baum Aug 03 '20 at 09:29
  • The point is that this answers suggests to compute the value *from the value*. There is absolutely no information gained from doing this. ``tup[tup.index(val)]`` is *guaranteed* to be equal to just ``val``. – MisterMiyagi Aug 03 '20 at 09:36
  • MisterMiyagi would answer better if he read the question. I stated I knew I could just pass in the values, but I wanted to grab specifics from a tuple, multiple tuples actually I just simplified the question before asking. Maybe too much. – Grave Aug 03 '20 at 09:39
  • So MisterMiyagi is right if he says it has no additional information, but I wanted to give Grave an overview of possible features with tuples. I edited my answer for further visitors. Still with the information of the index-method. – Sebastian Baum Aug 03 '20 at 09:42
  • @Grave The point is that using ``tup.index`` *also* requires being able to pass in the values, namely to ``tup.index``. – MisterMiyagi Aug 03 '20 at 09:42
  • Question answered a while ago, index, thanks guys. – Grave Aug 03 '20 at 09:44