Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - getting values from 2d arrays

I have a 2d array:

[[], ['shotgun', 'weapon'], ['pistol', 'weapon'], ['cheesecake', 'food'], []]

How do I call a value from it? For example I want to print (name + " " + type) and get

shotgun weapon

I can't find a way to do so. Somehow print list[2][1] outputs nothing, not even errors.

like image 978
Борис Цейтлин Avatar asked Dec 18 '25 00:12

Борис Цейтлин


1 Answers

>>> mylist = [[], ['shotgun', 'weapon'], ['pistol', 'weapon'], ['cheesecake', 'f
ood'], []]
>>> print mylist[2][1]
weapon

Remember a couple of things,

  1. don't name your list, list... it's a python reserved word
  2. lists start at index 0. so mylist[0] would give []
    similarly, mylist[1][0] would give 'shotgun'
  3. consider alternate data structures like dictionaries.
like image 193
sahhhm Avatar answered Dec 20 '25 16:12

sahhhm