Haj Sai
Haj Sai

Reputation: 311

Indexing a list in python and looping through these

I have a list:

{'5ogb.pdb': [[['ASN', 15.0, 'A'], 8.0], [['ASN', 26.0, 'A'], 12.0]]} 

and I am trying to loop through the:

['ASN', 15.0, 'A'] and ['ASN', 26.0, 'A']

How do I index to be able to loop through only these values?

Upvotes: 1

Views: 53

Answers (2)

Sufiyan Ghori
Sufiyan Ghori

Reputation: 18753

You can just loop through your dict (yes its a dictionary not a `list), and print first item,

x = {'5ogb.pdb': [[['ASN', 15.0, 'A'], 8.0], [['ASN', 26.0, 'A'], 12.0]]} 
for list_object in x['5ogb.pdb']:
        print(list_object[0])

# output,
['ASN', 15.0, 'A']
['ASN', 26.0, 'A']

Upvotes: 2

Tyler
Tyler

Reputation: 395

You currently have a dictionary that has one key-value pair. Get the list by saying dict[key] and you can loop through it with like this:

for list1, list2 in zip(dict[key][0], dict[key][1]):
    #more code

zip() lets you iterate through both lists at the same time.

Upvotes: 0

Related Questions