0

I am not all that good at programming but I will try to explain my problem as good as I can:

Basically I have a list of lists like this

persons = [["Peter", 22], ["Jon", 26], ["Ann", 27], ["Mark", 23], ["Susan", 30]]

and what I want is for something to happen if the persons name is for example 'Peter'.

So I would like to do:

if persons[0]=='Peter':
    # do_something

But persons[0] is also the age and not just the name.

How do I just get access to the name, like "if the name of persons[0] is Peter"?

j-i-l
  • 10,281
  • 3
  • 53
  • 70
Gabc
  • 89
  • 2
  • 9
  • 1
    Huh? `persons[0]` is `"Peter"`. `persons[1]` will be 22. – DavidG Jan 09 '18 at 14:28
  • It's not clear what you're asking and where you're having trouble. You can iterate through the list one by one and check if the name is `"Peter"`: `for person in persons: if person == "Peter": do_something` – pault Jan 09 '18 at 14:29
  • Might have presented my list wrong, but basically if I print persons[0] it prints out ['Peter', 22] – Gabc Jan 09 '18 at 14:34
  • I don't understand your question. But are you sure, a dictionary like `agedict = {"Peter": 22, "Jon": 26, "Ann": 27, "Mark": 23, "Susan": 30} ` wouldn't suit your purposes better here? – Mr. T Jan 09 '18 at 14:35
  • 2
    Ah, you have a list of list like `persons = [["Peter", 22], ["Jon", 26], ["Ann", 27], ["Mark", 23], ["Susan", 30]]`. You can access "Ann" with `x = persons[2][0]` – Mr. T Jan 09 '18 at 14:38
  • yes! thats what im after, thank you and sorry for the bad explanation! – Gabc Jan 09 '18 at 14:40
  • Though as @Piinthesky suggested, a dictionary could probably be better here – DavidG Jan 09 '18 at 14:41
  • 1
    Gabc, I'd suggest to edit your question to make clear, that you have a list of lists. As it is now, the question is really confusing. – Mr. T Jan 09 '18 at 14:45
  • Updated the question with a clearer problem statement (as I understand it). **The question is a duplicate of https://stackoverflow.com/questions/18449360/access-item-in-a-list-of-lists and should be closed**. – j-i-l Jan 09 '18 at 22:49

1 Answers1

2

The short answer after the edit:

To access the name, it is important to realize that persons[0] is also a list. So you need to access the first element of this list in order to get the name:

if persons[0][0] == 'Peter':
    # do something

It seems to me that your list persons has always a name and an age that belong together. For such type of structure a list might not be ideal and I would recommend using a dictionary.

To start to work with dictionaries you need to convert your list:

# cerate a dictionary
persons_dict = {persons[i]: persons[i+1] for i in range(len(persons))[::2]}
# or for the edited list of lists
persons_dict = {pers[0]: pers[1] for pers in persons}

Now you can write things like:

print(persons_dict['Peter'])

Or test conditions and access to corresponding age like so:

a_person = raw_input('Give the name of a person\n')
print('The age of {0} is {1}'.format(a_person, persons_dict[a_person]))
if a_person == 'Peter':
    print('You asked for Peter!')

If you want to work with lists, consider creating a list of lists (which you did in your edit):

persons = [[persons[i], persons[i+1]] for i in range(len(persons))[::2]]

now you can write:

if persons[0][0] == 'Peter':
    print('The age of {0} is {1}'.format(persons[0][0], persons[0][1]))
j-i-l
  • 10,281
  • 3
  • 53
  • 70