Python Forum
loop through list or double loop
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.
Reply
#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
Reply
#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
Reply
#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.
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Re Try loop for "net use..." failures tester_V 10 446 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 683 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 427 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 306 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 358 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Variable definitions inside loop / could be better? gugarciap 2 373 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 544 Dec-28-2023, 05:14 PM
Last Post: trix
  Need an alternative to brute force optimization loop jmbonni 5 1,106 Dec-07-2023, 12:28 PM
Last Post: RockBlok
  Which GUI can have indefinite loop ? jst 20 1,577 Nov-16-2023, 10:23 PM
Last Post: jst
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,524 Nov-07-2023, 09:49 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020