1

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.

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1
  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])

Comments

1

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)

Comments

1

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')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.