Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to access arguments by name from function
#1
Hi,

I have function in which I am passing multiple lists. Now, I want to access (or assign to another variable) the lists (given as arguments in function) by its name. I tried below code, but its giving error.

#%% passing multiple lsit to function
def my_func(list1,list2,list3):
    return list1,list2,list3
p1 = my_func(list1 = ['BNKL', 'KPUI', 'HYNI'],\
             list2 = ['Ty'],\
                  list3 = [6, 7, 8])
print(" The first list : ", p1[0])
print(" The second list : ", p1[list1])
l_l1 = p1[1]
Error:

The first list :  ['BNKL', 'KPUI', 'HYNI']
Traceback (most recent call last):

  File "<ipython-input-2-30d2f38dbdd7>", line 6, in <module>
    print(" The second list : ", p1[list1])

NameError: name 'list1' is not defined
Quote
#2
Names defined in the function are only available in the function. If you need them outside the function, return a dictionary:

def foo(list1, list2, list3):
    return {'list1': list1, 'list2': list2, 'list3': list3}
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
#%% passing multiple lsit to function
def my_func(list1,list2,list3):
    return {'list1': list1,'list2':list2,'list3': list3}
p1 = my_func(list1 = ['BNKL', 'KPUI', 'HYNI'],\
             list2 = ['Ty'],\
                  list3 = [6, 7, 8])
print(" The second list : ", p1['list2'])
print(" The first list : ", p1[0])
Error: Can't we acess by index?

The second list : ['Ty']
Traceback (most recent call last):

File "<ipython-input-7-9fcbb5cddfc7>", line 6, in <module>
print(" The first list : ", p1[0])

KeyError: 0
Quote
#4
Not with a dictionary. You could use a named tuple, but before I went that far I would ask if I really needed to access by both.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
Does it seem if we give in list format, then we can not access list by index?
Quote
#6
You can get it by index if you return the values as a nested list


def my_func(list1,list2,list3):
    return [list1,list2,list3]
p1 = my_func(list1 = ['BNKL', 'KPUI', 'HYNI'],\
             list2 = ['Ty'],\
                  list3 = [6, 7, 8])

print(" The first list : ", p1[0])
Quote
#7
Sir/Madam,
my apologies, I am still confused, for example: from the nested list, we can not access by name, but only access by index. In the previous answer below, we can only access by name. This conclusion is correct? or can we make it flexible that we can either access by index or by name?
def my_func(list1,list2,list3):
    return {'list1': list1,'list2':list2,'list3': list3}
Quote
#8
(Sep-19-2019, 03:06 PM)SriRajesh Wrote: can we make it flexible that we can either access by index or by name?

As I said, you would need to use a namedtuple. But why do you need to access it both ways? It creates an inconsistency, and inconsistencies cause problems down the road. It might be better to change how you are using the data than to change the data.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 160 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Pass Arguments to Function phillyfa 2 175 Mar-27-2020, 12:05 PM
Last Post: phillyfa
  Function with many arguments, with some default values medatib531 3 266 Mar-14-2020, 02:39 AM
Last Post: medatib531
  Using function *args to multiply multiple arguments allusernametaken 8 509 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  Passing Values of Dictionaries to Function & Unable to Access Them firebird 3 463 Aug-03-2019, 10:25 PM
Last Post: firebird
  readshapefile function in basemap - confusion regarding arguments sendiptangshu 1 540 Mar-09-2019, 09:29 AM
Last Post: Larz60+
  __init__ function arguments CoderMan 4 643 Jan-27-2019, 02:04 PM
Last Post: stullis
  Function Default Arguments grkiran2011 7 1,008 Aug-09-2018, 11:12 AM
Last Post: grkiran2011
  Function / Arguments / Error Help Ghosty 8 1,317 Jul-01-2018, 10:35 AM
Last Post: buran
  Supplying 2 arguments to 3-argument function valtih 3 1,550 Sep-10-2017, 11:10 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)