1

Here is the code snippet, which is unable to print items of the tuple. [Using python versions 2.6.8/2.7.10]

def lists (var, *st):
    print type(st)
    for item in range(1,len(st)):
        print "Items is:" + item
st = ['udf','var_udf']
lists("a",st)

Thanks in advance

Vaibhav Bajaj
  • 1,934
  • 16
  • 29
user3655447
  • 157
  • 1
  • 2
  • 11
  • 1
    Possible duplicate of [What does the Star operator mean in Python?](http://stackoverflow.com/questions/2921847/what-does-the-star-operator-mean-in-python) – TessellatingHeckler Jul 25 '16 at 04:52

2 Answers2

1

This would not be printing anything because you have used item in range(1,len(st)) giving item the value of an integer. Instead do something like:

for item in st:

CODE:

def lists (var, *st):
    print type(st)
    for item in st:
        print "Items is:"
        print ' '.join(item)
st = ['','udf/','var_udf']
lists("a",st)
Vaibhav Bajaj
  • 1,934
  • 16
  • 29
  • Thanks vaibhav. But, my actual requirement is to get the file-names using the itmes in that tuple. like create_udf.sql/create_var_udf.sql For this, def lists (var, *st): print type(st) for item in st: sql = "create_".join(item) + ".sql" print sql st = ['udf','var_udf'] lists("a",st) Giving output as: udfcreate_var_udf.sql Can you please help on this to achieve two sql file names – user3655447 Jul 25 '16 at 05:21
  • Then make sure the first element of st is always `''`and append `'/'` to all elements other than the last element. – Vaibhav Bajaj Jul 25 '16 at 05:26
0
a = [1,2,3,4,5,6,7,8,9] #list
print (a)

def b(list):
    for i in list:
        print (i) #each element of the list printed here

b(a) #calling def

If you are passing a list as an argument to the function, the below function will print each element of the list. If you want to perform any operations like append etc., you can add the code after for loop.

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67