Get item with value from tuple in python

user4005632

I have a tuple that I fetched from a query, but I'm not experienced with Python so I don't know the right/best way to do this. Here is what the tuple looks like:enter image description here

Now I need something like result.getItemWithKey('111.111.5.1') that returns a single array object or comma separated string ( as it's more useful) like 'object1, 111.111.5.1'

mhawke

You can search for a particular tuple in the results list by iterating over the list and checking the value of the second item of each tuple (which is your key):

results = [('object%d' % i, '111.111.5.%d' % i) for i in range(1,8)]

key = '111.111.5.4'
result = None
for t in results:
    if t[1] == key:
        result = t

print result

Output:

('object4', '111.111.5.4')

This demonstrates accessing an item in a tuple with a zero-based index (1 in this case means the second element). Your code will be more readable if you unpack the tuples in the for loop:

for obj, value in results:
    if value == key:
        result = (obj, value)

Your results might be more generally useful if you convert them to a dictionary:

>>> results_dict = {v:k for k,v in results}
>>> print results_dict['111.111.5.6']
object6
>>> print results_dict['111.111.5.1']
object1
>>> print results_dict['blah']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'blah'
>>> print results_dict.get('111.111.5.5')
object5
>>> print results_dict.get('123456')
None

Using dict.get() is close to the syntax that you requested in your question.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Group python tuple by first item

From Dev

How to get first item of each tuple in a list / python

From Dev

Best way to get the value from a tuple in C#

From Dev

Correct way to get single (first) value from functions that returns a tuple

From Dev

Get second item from Tuple which is List<T>

From Dev

Python - Return a value from a tuple

From Dev

from list of tuples, get tuple closest to a given value

From Dev

Python: get dictionary value from a dictionary within a tuple within a list and do something with it

From Dev

python get second value of a tuple in a list

From Dev

how to sort tuple value from list in python

From Dev

Get attribute value from a selected item in a list

From Dev

Python extract indices from tuples if second item in tuple is sequential

From Dev

Python: How to chain tuple in a list by the next item value

From Dev

Python: How to chain tuple in a list by the next item value

From Dev

get (multi)return value from a std::tuple function QtConcurrentRun

From Dev

Get Value of Tuple From MySQL into String Array

From Dev

How to get a custom value from an array item

From Dev

I am trying to compare an item from a tuple and a list but get an error

From Dev

Access value from tuple by key in python

From Dev

python get second value of a tuple in a list

From Dev

XAML Get Item text value from ListViewItem

From Dev

Extract specific number from tuple value in Python

From Dev

Get attribute value from a selected item in a list

From Dev

How to get a random item from a list, tuple, or string?

From Dev

Python get json array item by matching value

From Dev

Get value from another value in a tuple of tuples without using a for loop

From Dev

Get a tuple value in a tuple by index

From Dev

Get selected item value from ListView CustomAdapter

From Dev

Python, How do I get the average from a tuple from MySQL?

Related Related

  1. 1

    Group python tuple by first item

  2. 2

    How to get first item of each tuple in a list / python

  3. 3

    Best way to get the value from a tuple in C#

  4. 4

    Correct way to get single (first) value from functions that returns a tuple

  5. 5

    Get second item from Tuple which is List<T>

  6. 6

    Python - Return a value from a tuple

  7. 7

    from list of tuples, get tuple closest to a given value

  8. 8

    Python: get dictionary value from a dictionary within a tuple within a list and do something with it

  9. 9

    python get second value of a tuple in a list

  10. 10

    how to sort tuple value from list in python

  11. 11

    Get attribute value from a selected item in a list

  12. 12

    Python extract indices from tuples if second item in tuple is sequential

  13. 13

    Python: How to chain tuple in a list by the next item value

  14. 14

    Python: How to chain tuple in a list by the next item value

  15. 15

    get (multi)return value from a std::tuple function QtConcurrentRun

  16. 16

    Get Value of Tuple From MySQL into String Array

  17. 17

    How to get a custom value from an array item

  18. 18

    I am trying to compare an item from a tuple and a list but get an error

  19. 19

    Access value from tuple by key in python

  20. 20

    python get second value of a tuple in a list

  21. 21

    XAML Get Item text value from ListViewItem

  22. 22

    Extract specific number from tuple value in Python

  23. 23

    Get attribute value from a selected item in a list

  24. 24

    How to get a random item from a list, tuple, or string?

  25. 25

    Python get json array item by matching value

  26. 26

    Get value from another value in a tuple of tuples without using a for loop

  27. 27

    Get a tuple value in a tuple by index

  28. 28

    Get selected item value from ListView CustomAdapter

  29. 29

    Python, How do I get the average from a tuple from MySQL?

HotTag

Archive