Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 loop through list or double loop
#1
So. Brainfart here atm.

I have a list of which I don't if it consist out of (all) nested lists or just simple values.

So
listA = ["a", "b", "c"]
or
listA = [list1, list2 ]
list1=["a", "342"]
list2=["2fasd", "adf3gfds"]


If have to run a function I defined of those values of the single list hierachy (so listA (only the first example), list1, list2)

I can't check isinstance(listA, list) because both are lists. So I have to check if at all this is true, and if so: are there any nested lists. and if so: do that function there.

for i in list:
//do function

I've done some any(l in list) for l in listA but that does nothing.

Hope it makes sense.
Quote
#2
It's not clear what you're trying to do. Could you show some code, along with what the output is vs your expected output?

If it's just iterating over a list of lists, then a for loop works fine:
>>> fruit = ["spam", "eggs", "ham"]
>>> veggies = ["gross", "green", "not-chocolate"]
>>> consumables = [fruit, veggies]
>>> for sublist in consumables:
...   for item in sublist:
...     print(item)
...
spam
eggs
ham
gross
green
not-chocolate
Quote
#3
Yeah I figured, typing without example is bs, sorry for that. Let me try again.

#inputlist comes from another program which could either be a single list like inputlist_a or like inputlist_b BUT could also be a presented with nested lists like inputlist_c
inputlist_a = ["sheet1", "sheet2", "sheet3"]
inputlist_b = ["sheet124", "sheet1233", "sheet12373"]
inputlist_c = [a, b]

#since I don't know the input I'm going to name this INPUT


if isinstance(INPUT, list):
	for i in INPUT:
		if isinstance(i, list)
			for j in i:
				domyfunction(j) 
		else
			domyfunction(i)

#so in case of inputlist_a:
#domyfunction loops sheet1, sheet2, sheet3
#so in case of inputlist_b:
#domyfunction loops sheet124, sheet1233, sheet12373
#so in case of inputlist_c:
#domyfunction loops sheet1, sheet2, sheet3 THEN sheet124, sheet1233, sheet12373
Quote
#4
Does that not work for you? It looks fine, as long as things can only be nested one level. Any more than that, and I'd rather use a recursive function.
Quote
#5
aaannndd some coffee and backing away from my problem solved it.

The error I got was "expecting 'view' but got List[object]", now 'view' is a program-specific name but I was using a list. 100% sure.

Now:
I did use a list, my coding above did work.
HOWEVER I used a function which also has another variable. No problem, however I used a listobject there.

So I was looking at the wrong 'list'-issue.

Solution: for v, fp in zip(lista, listc) etc.etc.


pfff.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  removing items from a list or group within a for loop. allusernametaken 6 161 Today, 06:52 AM
Last Post: perfringo
  How to Display Multiple Time Tables With While Loop ZQ12 2 88 Yesterday, 04:15 AM
Last Post: ZQ12
  For loop not working pjgrah01 4 100 Nov-03-2019, 11:58 PM
Last Post: pjgrah01
  Help converting MATLAB triple-for loop to Python davlovsky 1 108 Oct-29-2019, 10:26 PM
Last Post: scidam
  Loop through folder of Excel Files and extract single column fioranosnake 2 96 Oct-28-2019, 05:19 PM
Last Post: fioranosnake
  Breaking While Loop JustWonderful 4 256 Oct-28-2019, 01:12 AM
Last Post: JustWonderful
  How does while-If-elif-else-If loop conditions check run mrhopeedu 2 115 Oct-27-2019, 04:56 AM
Last Post: mrhopeedu
  Do break operators turn while loop conditions from True to False? Drone4four 5 212 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019
  List and loop Sandeep2000 5 192 Oct-23-2019, 08:54 AM
Last Post: perfringo
  Print different positions in loop from functions konsular 5 141 Oct-16-2019, 08:10 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)