How can I access each element of a pair in a pair list?
Asked Answered
H

6

45

I have a list called pairs.

pairs = [("a", 1), ("b", 2), ("c", 3)]

And I can access elements as:

for x in pairs:
    print x

which gives output like:

('a', 1) ('b', 2) ('c', 3)

But I want to access each element in each pair, like in c++, if we use pair<string, int> we are able to access, first element and second element by x.first, and x.second.eg.

x = make_pair("a",1)
x.first= 'a'
x.second= 1

How can I do the same in python?

Housel answered 19/3, 2014 at 5:2 Comment(3)
Thanks alot you people. This was my first question at stackoverflow. Was not expecting such quick and correct replies. :)Housel
If the question, is good, then it doesn't matter if it's your first or not! ;)Endure
Something important to keep on your mental problem-solving checklist: Q. "I have data X, which comes from Y. How do I do Z with it?" A. "Well, how would you do Z with X if it didn't come from Y?" Each "pair" here is its own thing, and the fact that they were stored in a list before doesn't matter. All you need to know is what the actual type is (tuple) and then you can look up the documentation, or already know from having followed a tutorial.Klapp
D
50

Use tuple unpacking:

>>> pairs = [("a", 1), ("b", 2), ("c", 3)]
>>> for a, b in pairs:
...    print a, b
... 
a 1
b 2
c 3

See also: Tuple unpacking in for loops.

Des answered 19/3, 2014 at 5:8 Comment(0)
F
18

If you want to use names, try a namedtuple:

from collections import namedtuple

Pair = namedtuple("Pair", ["first", "second"])

pairs = [Pair("a", 1), Pair("b", 2), Pair("c", 3)]

for pair in pairs:
    print("First = {}, second = {}".format(pair.first, pair.second))
Functionalism answered 19/3, 2014 at 5:58 Comment(0)
F
13

A 2-tuple is a pair. You can access the first and second elements like this:

x = ('a', 1) # make a pair
x[0] # access 'a'
x[1] # access 1
Foetor answered 19/3, 2014 at 5:8 Comment(1)
It is not working for me both on python 2.7 and python 3. I am seeing this error: TypeError: tuple() takes at most 1 argument (2 given)Charitycharivari
S
2

When you say pair[0], that gives you ("a", 1). The thing in parentheses is a tuple, which, like a list, is a type of collection. So you can access the first element of that thing by specifying [0] or [1] after its name. So all you have to do to get the first element of the first element of pair is say pair[0][0]. Or if you want the second element of the third element, it's pair[2][1].

Sava answered 19/3, 2014 at 11:32 Comment(0)
F
1

I don't think that you'll like it but I made a pair port for python :) using it is some how similar to c++

pair = Pair
pair.make_pair(value1, value2)

or

pair = Pair(value1, value2)

here's the source code pair_stl_for_python

Ferrate answered 9/5, 2020 at 20:26 Comment(3)
What's the value of using your function over tuple unpacking, as suggested in the accepted answer? Tuple unpacking requires a lot less code and uses capabilities built into Python.Prairial
1. more familiar with c++'s pair STL \n 2. values are mutable :)Ferrate
even though your intentions are good, this is completely unnecessary. Coming from C++ shouldn't make someone continue writing C++ code in Python syntax, instead they should learn what Python's benefits are and how to make use of built-in capabilities more. Otherwise there wouldn't be any reason to learn any other language.Capitalist
D
-1

You can access the members by their index in the tuple.

lst = [(1,'on'),(2,'onn'),(3,'onnn'),(4,'onnnn'),(5,'onnnnn')]

def unFld(x):

    for i in x:
        print(i[0],' ',i[1])

print(unFld(lst))

Output :

1    on

2    onn

3    onnn

4    onnnn

5    onnnnn
Deutschland answered 18/10, 2017 at 14:41 Comment(1)
Even though this is a solution it is highly discouraged, since this is not how python is built. Thus, this approach is unwieldy and slower, than the approaches using the "for each" loop of python in it's intended way.Claudetteclaudia

© 2022 - 2024 — McMap. All rights reserved.