-1

I still have a hard time understanding list comprehensions.

Take for example the following list:

lst = ["Medical Center", "Playground", "Park", "Soccer Field", "Swim School", "Summer Camp", "Museum"]

Qn 1) How would one use list comprehension to iterate though the above list and check against a panda dataframe that has a hundred elements (lets say)?

Qn 2) I was wondering if there was a good resource that has more clarity on list comprehension; I have read some online resources, and while simpler operations are easier to understand, once it becomes a list in a list (in a list, in a list etc) then its as clear as mud

Anwarvic
  • 12,156
  • 4
  • 49
  • 69
Munish
  • 45
  • 7
  • Why would you use a list comprehension and padas to do something. Probably better to convert the list to a set and do some pandas iloc magic with it. – Patrick Artner May 27 '20 at 09:56
  • The objective of the question is not just about solving the challenge; I do realize there are other ways to solve it, but I was curious about list comprehensions, thats all. I wanted to find a way to do it, and more importantly, if someone could explain what it is they did – Munish May 27 '20 at 13:41
  • If you use list comp with pandas you void the speed the c++ pandas implenetationg gives you. Learning list comp is very usefull - but not for that. There are plenty of list comp posts on SO, f.e. https://stackoverflow.com/questions/20639180/explanation-of-how-nested-list-comprehension-works and https://stackoverflow.com/questions/18649884/python-list-comprehension-for-loops and https://stackoverflow.com/questions/37822872/convert-a-for-loop-to-a-list-comprehension and other ressources: https://www.youtube.com/watch?v=1HlyKKiGg-4 ... all easily findable by google. hth – Patrick Artner May 27 '20 at 16:10

1 Answers1

0

You can do something like this, if for example you only have elements starting with the letter 'P' in your dataframe, and use it to filter your list with a list comprehension:

import pandas as pd

lst = ["Medical Center",
       "Playground",
       "Park",
       "Soccer Field",
       "Swim School",
       "Summer Camp",
       "Museum"
       ]

# filtered_lst = [elem for elem in lst if elem.startswith('P')]

df = pd.DataFrame(["Playground", "Park"])

# Extract dataframe values into a flattened list:
df_values = [k for i in df.values for k in i]

# Compare original list elements to dataframe values:
common_elements = [elem for elem in lst if elem in df_values]

print(common_elements)

will result in:

['Playground', 'Park']
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53
  • 1
    Thanks @Gustav, this worked. Can you explain the list comprehension to me? How/Where can I learn more about this topic? – Munish May 27 '20 at 14:07
  • Awesome! I'm glad to hear it :) If you found this answer useful, please consider to mark it as the accepted answer. The Python list comprehension is just a short form for the usual for-loop and if-statements used to filter on element values etc., I found this tutorial useful: https://realpython.com/list-comprehension-python/ – Gustav Rasmussen May 27 '20 at 14:09
  • Updated the code as well, for greater readability – Gustav Rasmussen May 27 '20 at 14:15