# Apply a function to multiple lists?

**URL:** <https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287>\
**Category:** Questions\
**Tags:** symbolic-computations\
**Created:** [September 22, 2016, 3:41pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287 "2016-09-22T15:41:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [September 22, 2016, 3:41pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/1 "2016-09-22T15:41:28Z")

</div>

I have a few functions, specifically, volume, number of fish and cost per fish. Here they are:

```
vlm[nsides_, length_, depth_] := (1/4*nsides*length^2*Cot[\[Pi]/nsides]*depth);

numfish[nsides_, length_, depth_] := Floor[vlm[nsides, length, depth]/.25];

costperfish[pondcost_, nsides_, length_, depth_] := pondcost/numfish[nsides, length, depth];

```

.25 refers to the space(volume) one fish can take up(this number could change). The pond cost, number of sides, length and depth could essentially be any inputs. Rather than using the apothem(from the area of a polygon) for the volume function, I found another formula which contains the Cot function(the one I have above)..

I am trying to apply multiple list inputs to the cost per fish function. For example. costperfish[50,4,2.5,2] would result in the output 1, which is the cost per fish. I want to be able to type my function for multiple inputs, such as: costperfish[{50,4,2.5,2},{20,6,1,2.5}] and get the costs per each of these inputs. At some point I will need to apply a number to each cost, that will arrange the costs in order from least to greatest. So costperfish[{50,4,2.5,2},{20,6,1,2.5}] would essentially look like {{2,.8},{1,1.}}.. So I guess I asked 2 questions here but I am most concerned with inputting multiple lists so that the output will have each cost per fish for that input.

Any suggestions or advice would be greatly helpful. I don’t necessarily need an answer for the context of what I’m working on, even a simple example would help me in a more advanced problem, like this one.

---

<div class="post-metadata">

**Author:** ![lvveen](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/lvveen/32/4003_2.png) [@lvveen](https://community.wolfram.com/u/lvveen)\
**Post date:** [September 22, 2016, 4:02pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/2 "2016-09-22T16:02:52Z")

</div>

Use

```
Map[costperfish[#]&,{{50,4,2.5,2},{20,6,1,2.5}}]

```

---

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [September 22, 2016, 5:02pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/3 "2016-09-22T17:02:18Z")

</div>

Hi, thank you!

I tried this code and it just gives me the same output as the input.. I will mess around with it and get it right sooner or later.

I appreciate the help

---

<div class="post-metadata">

**Author:** ![jmgilly](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@jmgilly](https://community.wolfram.com/u/jmgilly)\
**Post date:** [September 22, 2016, 5:48pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/4 "2016-09-22T17:48:47Z")

</div>

Or…

```
testfunc[x_, y_, z_, t_] := x + y + z + t;

testfunc[#1, #2, #3, #4] & @@@ {{1, 2, 3, 4}, {5, 6, 7, 8}}

```

To me, it is not always intuitive as to how to use the “@” operator against lists, but interpreting the resultant error messages and trial and error often lead to the appropriate solution.

---

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [September 22, 2016, 6:07pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/5 "2016-09-22T18:07:23Z")

</div>

Joel,

This is fantastic. I appreciate the example, because I understand exactly how to apply this to my problem. I will explore the @ command because sure I got the output I needed, but not exactly WHY and WHAT its doing. In addition, I’m still trying to figure out the Map command like Ivan mentioned above.

Thanks for your method,

Brandon

---

<div class="post-metadata">

**Author:** ![lvveen](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/lvveen/32/4003_2.png) [@lvveen](https://community.wolfram.com/u/lvveen)\
**Post date:** [September 22, 2016, 7:03pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/6 "2016-09-22T19:03:25Z")

</div>

My bad 🙂  
You can do it in a lot of ways..

```
costperfish[{pondcost_, nsides_, length_, depth_}] := N[pondcost/numfish[nsides, length, depth]];
costperfishOriginal[pondcost_, nsides_, length_, depth_] := N[pondcost/numfish[nsides, length, depth]];

```

The costperfish has now a list as argument and that is exact what you want. Now it’s easy to map it!

```
data = {{50, 4, 2.5, 2}, {20, 6, 1, 2.5}, {30, 5, 1, 2.5}, {20, 7, 1, 2.5}}

Map[costperfish[#] &, data]

```

or

```
Map[Apply[costperfishOriginal, #] &, data]

```

both give  
{1., 0.8, 1.76471, 0.555556} as a result

---

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [September 22, 2016, 11:46pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/7 "2016-09-22T23:46:58Z")

</div>

Okay, I see now!

I appreciate your clarification.I haven’t used the Map command before but now I see what its doing. Thanks for clearing that up.

Brandon

---

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [September 23, 2016, 1:39am UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/8 "2016-09-23T01:39:31Z")

</div>

In using the input:

```
data := {{50, 4, 2.5, 2}, {20, 6, 1, 2.5}, {30, 5, 1, 2.5}, {20, 7, 1, 2.5}} ;
Map[costperfish[#1, #2, #3, #4] & @@@ data]

```

I get the output

```
Map[{1, 4/5, 30/17, 5/9}]

```

Almost as if Map didn’t even do anything. For some reason, I’m having trouble using the Map command, I might just not use it..Thanks again for all your help and patience!

Brandon

---

<div class="post-metadata">

**Author:** ![edmundr](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@edmundr](https://community.wolfram.com/u/edmundr)\
**Post date:** [September 23, 2016, 6:53pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/9 "2016-09-23T18:53:21Z")

</div>

You may use `Apply` with the _replaces heads at level 1_ shortcut.

```
costperfish @@@ {{50, 4, 2.5, 2}, {20, 6, 1, 2.5}}

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![lvveen](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/lvveen/32/4003_2.png) [@lvveen](https://community.wolfram.com/u/lvveen)\
**Post date:** [September 24, 2016, 7:49pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/10 "2016-09-24T19:49:27Z")

</div>

Hi Brandon,  
you’re using the map function with just 1 argument. that why it doesn’t evaluate further.  
I would suggest you to invest some time on learning the Map and Apply function because they are very frequently used!  
What helped me was the introduction of Mr Shifrin  
[http://www.mathprogramming-intro.org/](http://www.mathprogramming-intro.org/)  
and the video of Mr Gaylord are also very nice

> **[Wolfram Screencast & Video Gallery](https://www.wolfram.com/broadcast/s/?sx=gaylord)**

---

<div class="post-metadata">

**Author:** ![bdavis8594](https://avatars.discourse-cdn.com/v4/letter/b/e480ec/32.png) [@bdavis8594](https://community.wolfram.com/u/bdavis8594)\
**Post date:** [September 24, 2016, 9:51pm UTC](https://community.wolfram.com/t/apply-a-function-to-multiple-lists/9287/11 "2016-09-24T21:51:13Z")

</div>

Hey there,

That is the method I used, the function @@@ expression.

Thanks for the links also!

Best,

Brandon
