Find an element in a list of tuples in python [closed]

2024/7/7 4:57:02

I have the following list of tuples.

[('rel', 'dns-prefetch'), ('href', 'http://g-ecx.images-amazon.com')]
[('rel', 'dns-prefetch'), ('href', 'http://z-ecx.images-amazon.com')]   
[('rel', 'dns-prefetch'), ('href', 'http://ecx.images-amazon.com')]
[('rel', 'dns-prefetch'), ('href', 'http://completion.amazon.com')]
[('rel', 'dns-prefetch'), ('href', 'http://fls-na.amazon.com')]
[('rel', 'stylesheet'), ('href', 'http://z-ecx.images-  amazon.com/images/G/01/AUIClients/NavAuiAssets-bc93e610a616dd196eda0cc0238d74dd3f830199.min._V2_.css')]
[('rel', 'stylesheet'), ('href', 'http://z-ecx.images-amazon.com/images/G/01/AUIClients/AmazonUI-c0d0f2a115191b947c48fd97b453a95d32cbadc0.rendering_engine-not-trident.weblab-AUI_CSS_REDUCTION_28708-T1.weblab-AUI_HIGH_CONTRAST_40800-T1.min._V2_.css')]
[('rel', 'stylesheet'), ('href', 'http://z-ecx.images-amazon.com/images/G/01/AUIClients/AmazonGatewayAuiAssets-0320163872407ddbb58566fd72a492fca3b17ed4.min._V2_.css')]
[('rel', 'canonical'), ('href', 'http://www.amazon.com/')]

I need to find an element in those. I am doing.

 for attr in attrs:#Need a code here to find an element without iterating over attar.

Is there a way to do that in Python.

Answer

You can use in to test membership:

>>> for attr in attrs:
...   if ('href', 'http://fls-na.amazon.com') in attr:
...     print attr
...
[('rel', 'dns-prefetch'), ('href', 'http://fls-na.amazon.com')]
https://en.xdnf.cn/q/120427.html

Related Q&A

print dictionary values which are inside a list in python

I am trying to print out just the dict values inside a list in python.car_object = {}cursor = self._db.execute(SELECT IDENT, MAKE, MODEL, DISPLACEMENT, POWER, LUXURY FROM CARS)for row in cursor:objectn…

Triangle of numbers on Python

Im asked to write a loop system that prints the following:0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 0 1 2 3 4 5 0 1 2 3 4 0 1 2 3 0 1 2 0 1 0However, my script prints this:0 1…

Using regex to ignore invalid syntax [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

What is the requirements.txt file? What should be in it in this particular case?

I am switching from Replit to PebbleHost to host my Python bot. What do I put in my requirements.txt file? These are the imports that I have at the start of my bot. import asyncio import datetime impo…

Is this a linux or a virtualenv error?

I asked this question 4 days ago.Now, when I open a terminal, I see this:and this:Is it related to the initial problem i had, or it a python and virtualenv issue?

Python code to authenticate to website, navigate through links and download files

Im looking something which could be interesting to you as well. Im developing a feature using Python, which should be able to authenticate (using userid/password and/or with other preferred authentica…

How to get sum of products of all combinations in an array in Python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

How to get value from entry (Tkinter), use it in formula and print the result it in label

When using the function entry of Tkinter, you can write a string value and do things with it; but Im actually working with formulas. The idea is fairly simple: to put a bunch of boxes to fill with numb…

Printing tuples in a list

Im not getting how to proceed with this problem in lists,can any one help me out? Thanks in advance.input is :l = [(1,2),(3,4),(5,6)]output is:[(1,3,5),(2,4,6)]

Adding entries from multiple files in python

I have a question on how to add entries from 100 files (each file contains two columns) and then writing them to a new file(which will also contain two columns)?