-5

I have multiple lists. I want to run a for-loop on the items of each list

Attempt 1

foo = ["today", "tomorrow", "yesterday"] 
bar = ["What", "is", "chocolate"]
empty = []
for x in [foo, bar]:
    empty.append("mushroom" + x)
Runtime error 
Traceback (most recent call last):
  File "<string>", line 2, in <module>
TypeError: cannot concatenate 'str' and 'list' objects

Attempt 2

foo = ["today", "tomorrow", "yesterday"] 
bar = ["What", "is", "chocolate"] 
def shroom(x):
  print("mushroom" + x)
map(shroom, bar, foo)
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: shroom() takes exactly 1 argument (2 given)

My desired output would be two new variables, where each element in each list to have "mushroom" appended on.

fooMushroom = [mushroomtoday, mushroomtomorrow, mushroomyesterday]
fooBar = [mushroomWhat,mushroomis,mushroomchocolate]

Note: I do not want pairs using Zip

i.e., I do not want each element from each list to be paired, via zip or a similar function. I need the output of foo and bar to be saved in a variable seperately, not merged/paired.

G. Gip
  • 337
  • 1
  • 4
  • 10

2 Answers2

0

You can simply concatenate your list:

for x in foo + bar: empty.append("mushroom" + x)

I didn't see your wanted output. You could use dictionaries for clarity.

orig_data = {
"foo": ["a", "b", "c"],
"bar": ["a", "b", "c"]
}
mushroom_data = {}

for k,v in orig_data.iteritems():
    for x in v:
        if k not in mushroom_data:
            mushroom_data[k] = []
        mushroom_data[k].append("mushroom" + x)

print mushroom_data

Best regards.

Shiriru
  • 181
  • 1
  • 10
  • Thanks Shiriu, despite my initial code, i'm trying to save the output of foo and bar into two new variables (please see desired output in above post). Is this possible? – G. Gip Aug 09 '16 at 09:08
  • Yes, it is possible. You could iterate on a list of list etc. But I am not sure where is your issue there. Let me edit my answer. – Shiriru Aug 09 '16 at 09:11
  • When I run the new code, it returns this error code. `Parsing error SyntaxError: invalid syntax (line 2)` – G. Gip Aug 09 '16 at 12:13
  • @G.Gip Sorry, I did it quickly yesterday without testing. Here it should be working fine (tested this time) – Shiriru Aug 10 '16 at 02:31
-1

I know you said you didn't want to use zip, but in this way you get each element in its own variable, which is what you want no?

fooMushroom = []
barMushroom = []
for x, y in zip(foo, bar):
    fooMushroom.append("mushroom"+x)
    barMushroom.append("mushroom"+y)
dheiberg
  • 1,914
  • 14
  • 18
  • or list comprenhension: `["mushroom" + ' '.join(i) for i in zip(foo,bar)]` – Colonel Beauvel Aug 09 '16 at 09:05
  • Not quite. Please check desired output. – G. Gip Aug 09 '16 at 09:06
  • check my edit, simply changed the output but the basis remains the same – dheiberg Aug 09 '16 at 09:17
  • Unfortunately it becomes cumbersome to add a `append` for each list/variable, especially when you have multiple lists. The reason I want a for-loop/function solution is to prevent the need to add a `append` line for each list. Is there a way to automate it so the for-loop spits out fooMushroom, BarMushroom, without having to manually type append for each list in the loop? – G. Gip Aug 09 '16 at 12:19