How to remove quotes from list of strings and store it as list again..?

Question:

I have to call the function in a for loop. All those functions are stored in a list as a string with quotes. I need to remove those quotes and store the values again in a list.

What needs to be done:

  1. Fetch the function list from DB
  2. Remove single/double quotes from a list of strings
  3. Store those strings in a list
  4. Loop the list to execute functions

Python

fruits = ['apple','mango','orange']
print(type(fruits))
func = '[%s]'%','.join(map(str,fruits))
print(func) ## [apple,mango,orange]
print(type(func))

def apple():
  print("In apple")
def mango():
   print("In mango")
def orange():
   print("In orange")

n = len(func)
func_it = itertools.cycle(func)
for i in range(n):
   next(func_it)()

Output

<class 'list'>
[apple,mango,orange]
<class 'str'>

After removing the quotes from the strings, Its data type is getting changed to .
Is there any way to remove quotes from a list of strings and store those values as a list again?

Asked By: bluebud

||

Answers:

You can’t call functions like that. Removing quotes from a string won’t turn it into a function. You are constructing func to be '[apple, mango, orange]' which is a string. When you iterate over that you get each of the characters of the string. i.e you get [, a etc. Each is a string & you can’t call strings. You’re basically doing '['() which is meaningless.

Remember – in Python – functions are first-class objects. If you want to list over functions just put references to those functions in a list:

import itertools

def apple():
  print("In apple")
def mango():
   print("In mango")
def orange():
   print("In orange")

func = [apple, mango, orange]  # list of functions
n = len(func)
func_it = itertools.cycle(func)
for i in range(n):
    x = next(func_it)
    print(type(x))  # check the type
    x()

Which results in:

<class 'function'>
In apple
<class 'function'>
In mango
<class 'function'>
In orange

So if you want to construct this list from your string '[apple, mango, orange]' you need to eval it:

s = '[apple, mango, orange]'
func = eval(s)
print(func)

Which results in:

[<function apple at 0x000001FB9E7CF040>, <function mango at 0x000001FB9ECB7940>, <function orange at 0x000001FB9ECB7DC0>]

However if possible you should always try to avoid using eval

Answered By: rdas

Based on your code i’m guessing you want to call a function based on a string? I suggest using a dictionary

import itertools
fruits = ['apple','mango','orange']
def apple():
  print("In apple")
def mango():
   print("In mango")
def orange():
   print("In orange")
funcs = {'apple':apple()}
funcs['apple']

out

In apple
Answered By: Thavas Antonio

You can use globals() to get the function object using name then you can use that object

func = [globals()[fun] for fun in fruits]
func_it = itertools.cycle(func)
for i in range(len(func)):
   next(func_it)()

Output:

In apple
In mango
In orange
Answered By: deadshot

You can use the built in python exec() function that will execute any string as code.

#!/usr/bin/env python3

fruits = ['apple','mango','orange']

def apple():
  print("In apple")
def mango():
   print("In mango")
def orange():
   print("In orange")

for func in fruits:
    exec(func + '()')  

Output

In apple
In mango
In orange
Answered By: Nolan Walker

Here, the line hehe = eval(func) creates a list of the names which are later taken as function calls and it’s possible because the ending parentheses "()" are not necessary, atleast in Python 3.9.

import itertools

def apple():
  print("In apple")
def mango():
   print("In mango")
def orange():
   print("In orange")

fruits = ['apple','mango','orange']
print(type(fruits))
func = '[%s]'%','.join(map(str,fruits))
print(func) ## [apple,mango,orange]
hehe = eval(func)
print(type(hehe))

n = len(hehe)
func_it = itertools.cycle(hehe)
for i in range(n):
   next(func_it)()

output:

<class 'list'>
[apple,mango,orange]
<class 'list'>
In apple
In mango
In orange

Answered By: prashant_raj
    fruits = '[apple, mango, orange]'
    import json
    fruits= json.loads(fruits)

output:
fruits = [apple, mango, orange]

Answered By: Fazi Alnjd