{"id":20020,"date":"2021-09-06T13:33:23","date_gmt":"2021-09-06T08:03:23","guid":{"rendered":"https:\/\/python-programs.com\/?p=20020"},"modified":"2021-11-22T18:36:28","modified_gmt":"2021-11-22T13:06:28","slug":"python-program-to-get-first-element-of-each-tuple-in-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-get-first-element-of-each-tuple-in-a-list\/","title":{"rendered":"Python Program to get First Element of each Tuple in a List"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Modulo of Tuple Elements<\/a><\/p>\n Tuple in Python:<\/strong><\/p>\n A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running.<\/p>\n Given some random number of tuples in a list and the task is to get the first element of each tuple in a given tuple list.<\/p>\n For example<\/p>\n let the tuple list = [(1, 2),(3, 4)]<\/p>\n The first element of tuples in the given list = [1, 3]<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to get the first element of each tuple in a given tuple list:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If you are new to the Python Programming Language then practice using our Python Programming Examples for Beginners<\/a> as our expert team has designed them from scratch.<\/p>\n In the previous article, we have discussed Python Program for Modulo of Tuple Elements Tuple in Python: A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running. Given some random number of tuples in a list and the task is …<\/p>\nGiven Tuple list = [(1, 2), (3, 4)]<\/pre>\n
The first element of the each tuple in a given tuplelist [(1, 2), (3, 4)] = [1, 3]<\/pre>\n
Given Tuple list = [('p', 'q', 'r'), ('s', 't'), ('u', 'v')]<\/pre>\n
The first element of the each tuple in a given tuplelist [('p', 'q', 'r'), ('s', 't'), ('u', 'v')] = ['p', 's', 'u']<\/pre>\n
Program to get the First Element of each Tuple in a List in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the tuple list as static input and store it in a variable.\r\ngvn_tupl_lst = [(1, 2), (3, 4)]\r\n# Take an empty new list to store the first element of each tuple in a given tuple list.\r\nfst_tupl_elemnts = []\r\n# Loop in the given input tuple list using the for loop.\r\nfor elemnt in gvn_tupl_lst:\r\n # Inside the loop, append the first element of each tuple in a given tuple list to\r\n # the above initialized new list using the append() function.\r\n fst_tupl_elemnts.append(elemnt[0])\r\n# Print the new list to get the\u00a0first element of each tuple in a given tuple list.\r\nprint(\"The first element of the each tuple in a given tuplelist\",\r\n gvn_tupl_lst, \"=\", fst_tupl_elemnts)\r\n<\/pre>\n
The first element of the each tuple in a given tuplelist [(1, 2), (3, 4)] = [1, 3]<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the number of tuples in a tuple list as user input using the int(input())\r\n# function and store it in a variable\r\nnumtuples=int(input('Enter some random number of tuples = '))\r\n#Take an empty list say \"gvn_tupl_lst\" and store it in another variable.\r\ngvn_tupl_lst=[]\r\n#Iterate in the above-given number of times using the for loop.\r\nfor t in range(numtuples):\r\n# Inside the loop, enter the tuple elements using tuple(),map(),input(),and split() functions\r\n# and Store it in a variable. \r\n tupleelemts=tuple(map(int,input('Enter random tuple elements = ').split()))\r\n# Append the above elements to the \"gvn_tupl_lst\".\r\n gvn_tupl_lst.append(tupleelemts)\r\n# Take an empty new list to store the first element of each tuple in a given tuple list. \r\nfst_tupl_elemnts = []\r\n# Loop in the given input tuple list using the for loop.\r\nfor elemnt in gvn_tupl_lst:\r\n # Inside the loop, append the first element of each tuple in a given tuple list to\r\n # the above initialized new list using the append() function.\r\n fst_tupl_elemnts.append(elemnt[0])\r\n# Print the new list to get the first element of each tuple in a given tuple list.\r\nprint(\"The first element of the each tuple in a given tuplelist\",\r\n gvn_tupl_lst, \"=\", fst_tupl_elemnts)\r\n<\/pre>\n
Enter some random number of tuples = 2\r\nEnter random tuple elements = 7 8 9\r\nEnter random tuple elements = 4 5 6\r\nThe first element of the each tuple in a given tuplelist [(7, 8, 9), (4, 5, 6)] = [7, 4]<\/pre>\n
\n