Python Forum
loop through list with do something
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop through list with do something
#2
If the items in mylist are hashable, you could use functools.lru_cache
from functools import lru_cache
import itertools as itt

def myfunct(a):
    #do something with a
    #return something
    print('myfunct({})'.format(a))

myf = lru_cache()(myfunct)
 
mylist = [1,2,3,4,5]

for x, y in itt.combinations(mylist, 2):
    val = myf(x)
    print(x, y)
myf.cache_clear()
Output:
myfunct(1) 1 2 1 3 1 4 1 5 myfunct(2) 2 3 2 4 2 5 myfunct(3) 3 4 3 5 myfunct(4) 4 5
Reply


Messages In This Thread
loop through list with do something - by 3Pinter - Oct-08-2019, 11:21 AM
RE: loop through list with do something - by Gribouillis - Oct-08-2019, 03:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Appending to list of list in For loop nico_mnbl 2 2,349 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Append list into list within a for loop rama27 2 2,356 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  loop through list or double loop 3Pinter 4 3,417 Dec-05-2018, 06:17 AM
Last Post: 3Pinter
  Write a for loop on list of lists without changing the shape of the main list Antonio 3 3,741 Jun-19-2018, 02:16 AM
Last Post: ichabod801
  For looping over a list, editing the list from inside the loop? Krookroo 3 3,932 Sep-04-2017, 05:08 PM
Last Post: Krookroo
  How to change from printFacts ( ) to return a list & Loop over list when writing CSV Ivan1 14 8,222 Aug-30-2017, 12:14 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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