Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop to compare lists
#2
uses f-string, expects python 3.6 or newer

all_lists = [
    ['bill', 5, 'time', 'bowling', 45, 3.456, 'distance'],
    ['counter', 'fracture', 'distance', 45, 'james', 'Sig'],
    ['time', 'fracture']
]

# example compare all lists against first
for l_no in range(1, len(all_lists)):
    print(f'\nCompare all_lists[0] against all_lists[{l_no}]')
    print(list(set(all_lists[0]) & set(all_lists[l_no])))
output:
Output:
Compare all_lists[0] against all_lists[1] ['distance', 45] Compare all_lists[0] against all_lists[2] ['time']
Reply


Messages In This Thread
loop to compare lists - by KHas - Jan-31-2019, 09:42 PM
RE: loop to compare lists - by Larz60+ - Jan-31-2019, 10:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through values and compare edroche3rd 6 705 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Compare two lists (with intersections). wnesbv 0 994 Jul-06-2022, 09:07 AM
Last Post: wnesbv
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,888 Apr-01-2021, 07:57 PM
Last Post: deanhystad
Exclamation Compare values in a for loop. penahuse 1 2,380 Feb-22-2021, 07:01 AM
Last Post: buran
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,973 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,387 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 2,898 May-10-2020, 05:28 AM
Last Post: deanhystad
  Lists first item is a number however i cant compare it with an int getting syntax err Sutsro 4 2,436 Apr-22-2020, 10:22 AM
Last Post: Sutsro
  Help: for loop with dictionary and nested lists mart79 1 1,875 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Can't seem to figure out how to put all of the lists items from a loop into 1 list Cosmosso 4 2,795 Feb-21-2020, 02:40 PM
Last Post: Cosmosso

Forum Jump:

User Panel Messages

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