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
  Using a For Loop to subtract numbers from an input function. Anunderling 9 2,651 Sep-22-2025, 08:56 PM
Last Post: deanhystad
  Problem with For loop indentation Mormolyce 10 2,983 Sep-03-2025, 03:55 AM
Last Post: DeaD_EyE
  Break within an if statement inside a while loop not working Python_more_on 2 1,838 Sep-03-2025, 12:55 AM
Last Post: Pedroski55
  Error loop with Chatgpt sportak12 1 3,340 Aug-26-2025, 08:11 AM
Last Post: python_developer
  Returning data on button click by buttons created by a loop bradells 3 1,272 Apr-23-2025, 03:01 PM
Last Post: Pedroski55
  in c# create a loop counting from 0 to 5, consecutively Frankd 19 4,514 Apr-01-2025, 12:46 PM
Last Post: Frankd
  really new to python want to know how to do a loop pentopdmj 6 2,589 Mar-09-2025, 12:59 PM
Last Post: snippsat
  knowing for loop position in a list medic5678 4 1,514 Jan-31-2025, 04:19 PM
Last Post: perfringo
  Run this once instead of a loop, do I need the 'calibration' steps? duckredbeard 2 1,320 Jan-28-2025, 04:55 PM
Last Post: duckredbeard
  How to convert while loop to for loop in my code? tatahuft 4 1,614 Dec-21-2024, 07:59 AM
Last Post: snippsat

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.