GFG App
Open App Browser
Continue

# How to extract a particular column from 1D array of tuples?

In this article, we will cover how to extract a particular column from a 1-D array of tuples in python.

Example

Input:  [(18.18,2.27,3.23),(36.43,34.24,6.6),(5.25,6.16,7.7),(7.37,28.8,8.9)]

Output: [3.23, 6.6 , 7.7 , 8.9 ]

Explanation: Extracting the 3rd column from 1D array of tuples.

## Method 1: Using Slice

As a first step let us first define a 1D array of tuples with each tuple having 3 elements, if we consider these 3 elements as 3 columns, we can use the slicing technique to extract a particular column.

## Python3

 `import` `numpy as np`   `# define a 1d array of tuples` `arr ``=` `np.array([(``18.18``, ``2.27``, ``3.23``), (``36.43``, ``34.24``, ``6.6``),` `                ``(``5.25``, ``6.16``, ``7.7``), (``7.37``, ``28.8``, ``8.9``)])`   `# slice the array by passing the ` `# column number` `arr[:, ``2``]`

Output:

`array([3.23, 6.6 , 7.7 , 8.9 ])`

## Method 2: Using the lambda function

In this example, we are taking a pandas data frame and one of the columns is an array of tuples, we can slice that particular column and apply a lambda function to extract a particular column from the tuple of an array.

## Python3

 `import` `numpy as np` `import` `pandas as pd`   `data ``=` `pd.DataFrame({``'approval'``: [``10``, ``20``, ``30``, ``40``, ``50``],` `                     ``'temperature'``: [(``18.18``, ``2.27``, ``3.23``), ` `                                     ``(``36.43``, ``34.24``, ``6.6``),` `                                     ``(``5.25``, ``6.16``, ``7.7``),` `                                     ``(``7.37``, ``28.8``, ``8.9``),` `                                     ``(``12``, ``23``, ``3``)]})`   `res ``=` `data[``'temperature'``].``apply``(``lambda` `x: x[``2``]).values`   `print``(data)` `print``(res)`

Output:

```   approval          temperature
0        10  (18.18, 2.27, 3.23)
1        20  (36.43, 34.24, 6.6)
2        30    (5.25, 6.16, 7.7)
3        40    (7.37, 28.8, 8.9)
4        50          (12, 23, 3

The output for extracting 3rd column from the array of tuples
[3.23 6.6  7.7  8.9  3.  ]```

## Method 3: Using list comprehension

A list comprehension can be used to extract a particular element from each tuple in the list and create a new list with the extracted elements.

In this example, we have the following list of tuples, we can use a list comprehension to extract the second element from each tuple (index 1) as follows:

## Python3

 `data ``=` `[(``'Alice'``, ``25``, ``'NYC'``), (``'Bob'``, ``30``, ``'Chicago'``), (``'Eve'``, ``35``, ``'SF'``)]` `ages ``=` `[age ``for` `name, age, city ``in` `data]` `print``(ages)`

Time Complexity: O(n)

Space Complexity: O(1)

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.64940476,"math_prob":0.94600207,"size":2210,"snap":"2023-14-2023-23","text_gpt3_token_len":803,"char_repetition_ratio":0.13009973,"word_repetition_ratio":0.13425925,"special_character_ratio":0.42941177,"punctuation_ratio":0.27287853,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9959132,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-09T05:01:01Z\",\"WARC-Record-ID\":\"<urn:uuid:cd01ebec-1500-4461-8c86-e4026820b10a>\",\"Content-Length\":\"278710\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0521924d-a2ab-4ebb-8b18-0c04fe8d75e0>\",\"WARC-Concurrent-To\":\"<urn:uuid:81129e1c-f4fb-49e9-a9c4-8169b9c32408>\",\"WARC-IP-Address\":\"44.228.100.190\",\"WARC-Target-URI\":\"https://origin.geeksforgeeks.org/how-to-extract-a-particular-column-from-1d-array-of-tuples/\",\"WARC-Payload-Digest\":\"sha1:KOYR2L4UCFWB4QIKASH3ITSNQB74QJC2\",\"WARC-Block-Digest\":\"sha1:Y67N7DSFBJEPGRJESNOTFABEIQHZEW47\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224655247.75_warc_CC-MAIN-20230609032325-20230609062325-00777.warc.gz\"}"}