-5

I have this list :

>>print(new_list)

[<None></None>]
[<None></None>, <img border="0" data-original-height="855" data-original-width="1885" src="https://1.bp.blogspot.com/-mq2ilVOcyPQ/X70khVD9UaI/AAAAAAAArLw/xC2LggPdcRUTm3aTGpPFYhoM6rDJwbyzACLcBGAsYHQ/s16000-rw/ssc-admit-card.webp"/>]
[<None></None>]

When I try to loop :

 for p in new_list:
    print(p['src'])

I get this error :

TypeError: 'NoneType' object is not subscriptable

How can I loop and get the items without the error?

Aelius
  • 1,029
  • 11
  • 22
Sainita
  • 332
  • 1
  • 4
  • 16
  • You need to review how you're populating *new_list* –  Nov 12 '21 at 08:26
  • You've asked pretty much the same question 2 hours ago: https://stackoverflow.com/questions/69938706/ the difference being that now you do not discard the exception by wrapping your code in a try..except – Mike Scotty Nov 12 '21 at 08:27
  • Does this answer your question? [How to Loop a List and Extract required data (Beautiful Soup)](https://stackoverflow.com/questions/69938706/how-to-loop-a-list-and-extract-required-data-beautiful-soup) – Mark Rotteveel Nov 12 '21 at 08:31
  • no, i am getting the items, but needed to extract the only src part, and not the whole item – Sainita Nov 12 '21 at 08:32
  • 2
    Please post a [mcve]. – molbdnilo Nov 12 '21 at 08:33
  • Duplicate of https://stackoverflow.com/questions/8949252/why-do-i-get-attributeerror-nonetype-object-has-no-attribute-something – wovano Nov 14 '21 at 09:10

1 Answers1

1

Don't try to print the Nones, and only print the elements with a 'src' attribute.

for p in new_list:
    if p and p['src']:
        print(p['src'])
molbdnilo
  • 64,751
  • 3
  • 43
  • 82