-2

I have two list :

list_1 = ['rwww corp\pan 2323 2018-23-23 0% c:\program files\pa.txt', 'rwww corp\pand 2323 2018-23-23 0% c:\program files\monkey.txt']

list_2 = ['c:\program files\pa.txt']

I would like to find if list_2 element is there in list_1, if yes, then print that.

In this case, list_2 element is at index 0 for list_1, how can i use one liner to achieve this?

I have a boring way of doing this by using two for-loops:

for e in list_1:
    for k in list_2:
          if k in e:
               print e
PanDe
  • 831
  • 10
  • 21
  • 6
    Why do you want to use `list comprehension` here? It is basically used to create new lists. You can use it but you shouldn't. It will also have the same for loops with condition. – hygull Jun 11 '18 at 17:10
  • Not relavant to your question, but I notice you use \ characters in the string for a directory separator - this is dangerous if the character following happens to be a special character, like b, n, r. Use a raw string, two backslashes, or a forward slash /. – cdarke Jun 11 '18 at 17:11
  • 1
    Do you actually want to only print the elements here, or do you want to store them in a new list? You should only be using a list comprehension if it's the latter. Comprehensions for side effects only (such as printing) are discouraged - see: https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects – sjw Jun 11 '18 at 17:12
  • What's wrong with `if list_2[0] in list_1:`? If you have more, then convert to a `set` and use the `set` operations – cdarke Jun 11 '18 at 17:15
  • the list shown here are for example, i have a big list, not possible to mention their index. – PanDe Jun 11 '18 at 17:15

3 Answers3

1

This would be the equivalent of your loop using list comprehension:

[print e for e in list_1 for k in list_2 if k in e]
spejsy
  • 123
  • 8
  • 2
    It is generally frowned upon to use a list comprehension for side effects, e.g. `print` – AChampion Jun 11 '18 at 17:29
  • **@spejsy**, have you properly tested your code. As I've checked it. It doesn't give proper result. You are picking 2 items from 2 lists and using `in`(which is basically used with list, tuple, dict etc.). – hygull Jun 11 '18 at 17:35
1

First I would look into itertools.product() and then I would not use a list comprehension for side effects, e.g.:

import itertools as it

print '\n'.join(e for e, k in it.product(list_1, list_2) if k in e)

I would also consider looking to the __future__ and using the print_function, e.g.:

from __future__ import print_function
import itertools as it

print('\n'.join(e for e, k in it.product(list_1, list_2) if k in e))
AChampion
  • 29,683
  • 4
  • 59
  • 75
0

@Petpan, you can use list comprehension to fulfill your need.

You should not use list comprehension for this.

Try it online at http://rextester.com/QBQDO38144.

list_1 = ['rwww corp\pan 2323 2018-23-23 0% c:\program files\pa.txt', 
          'rwww corp\pand 2323 2018-23-23 0% c:\program files\monkey.txt', 
          'c:\program files\pa.txt',
          'c:\program files\pex2.txt',
          'c:\program files\pex5.txt',
          'c:\program files\pex4.txt',
          ]

list_2 = ['c:\program files\pa.txt', 'c:\program files\pex2.txt', 'c:\program files\pex3.txt',]

# You should not use list comprehension for this purpose
# It will print items in list_1 if it also exists in list_2
[print(e, 'exists in list_1') for e in list_2 if e in list_1]

» Output

c:\program files\pa.txt exists in list_1
c:\program files\pex2.txt exists in list_1
hygull
  • 8,464
  • 2
  • 43
  • 52