Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing 2 lists
#1
Hello dear readers,

I'm very new to Python, I started reading and following some tutorials on Python 2 days ago. A long long time ago, like 10years ago, I used to code PHP and make websites with html, js, php, mySQL and css just came up. I didn't follow thrue with coding or programming ever since. 
Now I recently changed job and have some spare time I decided to pick up coding/programming again and found myself pretty interested in Python, due to the great accescibility and maybe potential job possibilities in the future.

I've been trying out some code and it's in a very early stage, it works but just since I'm really not familiar with Python functionality or possibilities I was just wondering if there's a way to optimise my code. In a later stage I'm going to implement some way to load the lists of names in the Variables "RuilenVanaf" & "RuilenNaar" but since I'm just learning I dont feel the need to do this right away, I just try to implement what I've learned from tutorials into code.

What the code should do: Compare what coworkers are in the list "RuilenVanaf" and what coworkers are in the list "RuilenNaar", if the match they should be stored in "Ruilen", pretty straight forward. Any way of optimising this code? Or any tips at al?

RuilenVanaf  = ["vivian", "dalton", "pim", "Tom", "Raymond", "Roy", "Leon", "Beckey", "Ridha", "Kevin", "Sjaak"]
RuilenNaar = ["vivian", "eyewumi", "dalton", "pim", "floor", "Roy", "Beckey"]
iAantalNaar = len(RuilenNaar)
iAantalVanaf = len(RuilenVanaf)
Ruilen = []
if iAantalNaar > iAantalVanaf:
   n = iAantalNaar
   s = iAantalVanaf
   for n in range(n):
       #   print(n)
       if RuilenNaar[n] in RuilenVanaf:
           # print(RuilenNaar[n], "match")
           Ruilen.append(RuilenNaar[n])
           continue
           #   else:
           #       print(RuilenNaar[n], " geen match")
else:
   n = iAantalVanaf
   s = iAantalNaar
   for n in range(n):
       #   print(n)
       if RuilenVanaf[n] in RuilenNaar:
           # print(RuilenVanaf[n], "match")
           Ruilen.append(RuilenVanaf[n])
           continue
           #   else:
           #       print(RuilenVanaf[n], " geen match")

print(Ruilen)

[Ashamed] I can't find an edit button, fyi the outcome of this code is:

['vivian', 'dalton', 'pim', 'Roy', 'Beckey']
Reply
#2
I believe that in this case your code could be optimised as:
Ruilen = list(set(RuilenNaar) & set(RuilenVanaf))
Converting lists to sets to do some operation like union, intersection or set difference is quite common practice in python.
 
Sometimes its not applicable (when you need to preserve order of elements or elements of your list cant be stored in a set). If you need to iterate over list, dont use for i in range(len(my_list)), but iterate directly over that list, for more read Never use for i in range... Your code on lines 7-16 and 18-27 is basically same - that suggests that perhaps using some function that accept two lists could be a good idea, after that you can use:
if len(list_a) > len(list_b):
   list_c = func(list_a, list_b)  # func is function that accepts two lists as arguments
else:
   list_c = func(list_b, list_a)
without repeating same code.

Or you could just swap your variables and use same code to compare them:
if len(list_a) > len(list_b):
    list_a, list_b = list_b, list_a       # if list_a is longer than list_b, swap them

for item in list_a:  # code that suppose that len(list_a) <= len(list_b) - now it is
    do stuff ...
Actually your code would work regardless of iteration over longer or shorter list, so you could remove "half" of it.
Reply
#3
I guess this is not school home work,zivoni have given a solution with sets.
List comprehensions is very common in Python and it's nice for this.
>>> [i for i in RuilenVanaf if i in RuilenNaar]
['vivian', 'dalton', 'pim', 'Roy', 'Beckey']
To understand it you can try to write is as a ordinary loop,
where you append result to a list.
Reply
#4
Thank you very much guys, cleared up a lot :)

RuilenNaar  = ["vivian", "dalton", "pim", "Tom", "Raymond", "Roy", "Leon", "Beckey", "Ridha", "Kevin", "Sjaak"]
RuilenVanaf = ["vivian", "eyewumi", "dalton", "pim", "floor", "Roy", "Beckey"]

Ruilen = []
for n in RuilenNaar:
   if n in RuilenVanaf:
       Ruilen.append(n)
print(Ruilen)
Lol I already love Python ;)
Reply
#5
I have nothing to add here - besides a little advice about naming conventions, and it's not even PEP-8.

Names of variables/classes/functions are part of your code documentation, therefor it's a good practice to provide meaningful English words/phrases as those. Finnish, Spanish, Russian and Hebrew are not recommended Tongue .

Even if you write code for your own fun - when you place it on forums for help, it helps your potential helpers.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  comparing two lists Siylo 19 6,974 Jan-23-2019, 02:09 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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