Accessing a specific string Python List

Question:

I have a list in python and I am trying to print out the list’s contents that have Brooklyn in it. I am running into a wall of somewhat and I am not sure how to go about getting this answer. Can someone help me?

list_woo=[]

 list_woo[0] = ['BROOKLYN', 'PETIT LARCENY OF MOTOR VEHICLE']
 list_woo[1] = ['BROOKLYN', 'OFFENSES AGAINST PUBLIC ADMINI']
 list_woo[2] = ['MANHATTAN', 'ASSAULT 3 & RELATED OFFENSES']
 list_woo[3] = ['BROOKLYN', 'ROBBERY']

if ‘BROOKLYN’ is in list_woo

Print out all contents that have Brooklyn in it, and the corresponding crimes. So the only one that should not show is MANHATTAN when you print it.

Asked By: genshuwoodswind

||

Answers:

  list_woo=[['BROOKLYN', 'PETIT LARCENY OF MOTOR VEHICLE'],
      ['BROOKLYN', 'OFFENSES AGAINST PUBLIC ADMINI'],
      ['MANHATTAN', 'ASSAULT 3 & RELATED OFFENSES'],
      ['BROOKLYN', 'ROBBERY']]

   # Solution 1
   for i in range(len(list_woo)):
      if list_woo[i][0]=='BROOKLYN':
          print(list_woo[i][1])

   import numpy as np

   # Solution 2
   array_woo = np.array(list_woo)
   indexArr = np.argwhere(array_woo == 'BROOKLYN')
   for i in indexArr:
     print(array_woo[i[0],1])
Answered By: Eirini Kotzia

This should work:

list_woo=[]

list_woo.append(['BROOKLYN', 'PETIT LARCENY OF MOTOR VEHICLE'])
list_woo.append(['BROOKLYN', 'OFFENSES AGAINST PUBLIC ADMINI'])
list_woo.append(['MANHATTAN', 'ASSAULT 3 & RELATED OFFENSES'])
list_woo.append(['BROOKLYN', 'ROBBERY'])

for borough, crime in list_woo:
    if borough == 'BROOKLYN':
        print([borough, crime])

Your original code won’t work because it initializes list_woo to be an empty list with no items and then attempts to update items at specific indices. Instead, you must either use append() to "grow" the list as in this answer or initialize using something like list_woo = [None] * 4 so that you allocate in advance the space necessary to place items at indices 0 through 3.

Answered By: constantstranger

Im not quite sure if this is what you want, but you can use filter, and modify this code to get the desired behavior:

list_woo=[]

list_woo.append(['BROOKLYN', 'PETIT LARCENY OF MOTOR VEHICLE'])
list_woo.append(['BROOKLYN', 'OFFENSES AGAINST PUBLIC ADMINI'])
list_woo.append(['MANHATTAN', 'ASSAULT 3 & RELATED OFFENSES'])
list_woo.append(['BROOKLYN', 'ROBBERY'])

filtered_list = filter(lambda element: 'BROOKLYN' in element[0], list_woo)

for val in filtered_list:
    print(val)
Answered By: Matías Rivas

You can use comprex lists for get that.

list_woo = []

list_woo.append(['BROOKLYN', 'PETIT LARCENY OF MOTOR VEHICLE'])
list_woo.append(['BROOKLYN', 'OFFENSES AGAINST PUBLIC ADMINI'])
list_woo.append(['BROOKLYN', 'ASSAULT 3 & RELATED OFFENSES'])
list_woo.append(['BROOKLYN', 'ROBBERY'])


print(*[element for element in list_woo if element[0] == 'BROOKLYN'], sep='n')
Answered By: Floyd
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.