2

I have two lists:

yy = ['Inside the area', 'Inside the area', 'Inside the area', 'Inside the area', 'Inside the area', 'Inside the area', 'Outside the area']
lat_ = [77.2167, 77.25, 77.2167, 77.2167, 77.2, 77.2167, 77.2]

I am combining them using the solution given on some posts here by:

new_dict = {k: v for k, v in zip(yy, lat_)}
print new_dict

But my output is {'Inside the area': 77.2167, 'Outside the area': 77.2}

This is happening because the most of the keys are same. I am doing this because I want to map these two list in order to get which values of lat falls inside or outside, and then keep only the ones which fall inside.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Raghav Patnecha
  • 716
  • 8
  • 29
  • If the `inside the area` key needs to have many values, why are you trying to use a dictionary ? If you still want to, you could make lists for dictionary values, which could hold multiple values for one key. – Rockybilly Feb 11 '18 at 09:19
  • But then the problem would be this: consider the situation when the key Inside are maps to a 77.216 and at the same time the key outside are maps to same value. Please note both the lists are a output of two different functions.That is the reason I am not combining it – Raghav Patnecha Feb 11 '18 at 09:25

2 Answers2

2

You can zip them and add a guard inside a list comprehension:

res = [lat for a, lat in zip(yy, lat_) if a.startswith(“Inside”)]
Netwave
  • 40,134
  • 6
  • 50
  • 93
1

To keep the values that are inside:

Code:

for k, v in zip(yy, lat_):
    if k.startswith('Inside'):
        inside.append(v)

Test Code:

yy = ['Inside the area', 'Inside the area', 'Inside the area',
      'Inside the area', 'Inside the area', 'Inside the area',
      'Outside the area']
lat_ = [77.2167, 77.25, 77.2167, 77.2167, 77.2, 77.2167, 77.2]

inside = []
for k, v in zip(yy, lat_):
    if k.startswith('Inside'):
        inside.append(v)
print(inside)

Results:

[77.2167, 77.25, 77.2167, 77.2167, 77.2, 77.2167]
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135