Skip to main content
added 2 characters in body
Source Link
Sociopath
  • 13.4k
  • 22
  • 53
  • 82
name ="bar" list = [["foo", 1], ["bar", 2], ["baz", 3]] new_list=[] for item in list: new_list.append(item[0]) print(new_list) try: location= new_list.index(name) except: location=-1 print (location) 

This accounts for if the string is not in the list too, if it isn't in the list then location = -1location = -1

name ="bar" list = [["foo", 1], ["bar", 2], ["baz", 3]] new_list=[] for item in list: new_list.append(item[0]) print(new_list) try: location= new_list.index(name) except: location=-1 print (location) 

This accounts for if the string is not in the list too, if it isn't in the list then location = -1

name ="bar" list = [["foo", 1], ["bar", 2], ["baz", 3]] new_list=[] for item in list: new_list.append(item[0]) print(new_list) try: location= new_list.index(name) except: location=-1 print (location) 

This accounts for if the string is not in the list too, if it isn't in the list then location = -1

Source Link
Coder123
  • 89
  • 1
  • 1

name ="bar" list = [["foo", 1], ["bar", 2], ["baz", 3]] new_list=[] for item in list: new_list.append(item[0]) print(new_list) try: location= new_list.index(name) except: location=-1 print (location) 

This accounts for if the string is not in the list too, if it isn't in the list then location = -1