41

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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
impossible
  • 2,380
  • 8
  • 36
  • 60
  • 2
    Thanks alot you people. This was my first question at stackoverflow. Was not expecting such quick and correct replies. :) – impossible Mar 19 '14 at 05:13
  • If the question, is good, then it doesn't matter if it's your first or not! ;) – gsamaras May 11 '16 at 08:46
  • 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. – Karl Knechtel Apr 08 '21 at 09:19

6 Answers6

47

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.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
15

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))
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
10

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
Jasmijn
  • 9,370
  • 2
  • 29
  • 43
Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52
  • 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)` – syam Oct 01 '19 at 16:38
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].

henrebotha
  • 1,244
  • 2
  • 14
  • 36
2

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

  • 2
    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. – Jeremy Caney May 10 '20 at 02:25
  • 1. more familiar with c++'s pair STL \n 2. values are mutable :) – Baraa Al-Masri May 10 '20 at 11:41
  • 3
    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. – sertsedat May 10 '20 at 17:49
-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
jrtapsell
  • 6,719
  • 1
  • 26
  • 49
  • 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. – mike Oct 24 '18 at 19:41