{"id":8090,"date":"2024-03-19T06:53:34","date_gmt":"2024-03-19T06:53:34","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=8090"},"modified":"2024-03-19T06:53:34","modified_gmt":"2024-03-19T06:53:34","slug":"python-list-comprehension","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-list-comprehension\/","title":{"rendered":"Python List Comprehension"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-python-list-comprehension\">What is Python List Comprehension?<\/a><\/li><li ><a href=\"#what-is-python-list-comprehension-syntax\">What is Python List Comprehension Syntax?<\/a><\/li><li ><a href=\"#list-comprehension-in-python-example\">List Comprehension in Python Example<\/a><\/li><li ><a href=\"#what-is-the-example-of-iteration-with-list-comprehension\">What is the Example of Iteration with List Comprehension <\/a><\/li><li ><a href=\"#what-is-an-example-of-an-even-list-using-list-comprehension\">What is an example of an Even List Using List Comprehension?<\/a><\/li><li ><a href=\"#what-is-matrix-using-the-list-comprehension\">What is Matrix using the List Comprehension?<\/a><\/li><li ><a href=\"#what-is-the-difference-between-list-comprehension-vs-for-loop\">What is the difference between List Comprehension vs For Loop?<\/a><\/li><li ><a href=\"#what-is-time-analysis-in-list-comprehensions-and-loops\">What is Time Analysis in List Comprehensions and Loops?<\/a><\/li><li ><a href=\"#what-is-nested-list-comprehension\">What is Nested List Comprehension?<\/a><\/li><li ><a href=\"#what-is-list-comprehensions-and-lambda\">What is List Comprehensions and Lambda<\/a><\/li><li ><a href=\"#conditionals-in-the-list-comprehension\">Conditionals in the List Comprehension<\/a><\/li><li ><a href=\"#what-is-the-example-of-python-list-comprehension-using-the-if-else\">What is the Example of Python List Comprehension using the if-else<\/a><\/li><li ><a href=\"#what-is-the-example-of-nested-if-with-the-list-comprehension\">What is the Example of Nested  IF  with the List Comprehension?<\/a><\/li><li ><a href=\"#display-the-square-of-numbers-from-1-to-10\">Display the square of numbers  from 1 to 10<\/a><\/li><li ><a href=\"#what-is-the-example-to-display-transpose-of-2-d-matrix\">What is the Example to  Display Transpose of 2D- Matrix?<\/a><\/li><li ><a href=\"#toggle-the-case-of-each-character-in-the-string\">Toggle the case of each character in the string<\/a><\/li><li ><a href=\"#what-is-the-example-to-reverse-each-string-in-a-tuple\">What is the example to reverse each string in a Tuple?<\/a><\/li><li ><a href=\"#what-is-the-example-of-creating-the-list-of-tuples-from-the-two-separate-lists\">What is the Example of creating the list of Tuples from the two separate Lists?<\/a><\/li><li ><a href=\"#what-is-the-example-to-display-the-sum-of-digits-of-all-the-odd-elements-in-the-list\">What is the Example to display the sum of digits of all the odd elements in the list?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-list-comprehension-fa-qs\">Python List Comprehension- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/www.skillvertex.com\/blog\/python-loop-lists\/\" data-type=\"post\" data-id=\"8077\">Python <\/a>list comprehension involves a bracket with the expression and will be executed for each element and the for loop to iterate over each element in the Python list. Read this article to learn more about Python List Comprehension.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-list-comprehension\">What is Python List Comprehension?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/www.skillvertex.com\/blog\/python-remove-list-items\/\" data-type=\"post\" data-id=\"8071\">List <\/a>Comprehension will provide the shorter <a href=\"https:\/\/www.skillvertex.com\/blog\/python-syntax\/\" data-type=\"post\" data-id=\"6924\">syntax <\/a>and will make a new list depending on the <a href=\"https:\/\/www.skillvertex.com\/blog\/how-can-i-return-multiple-values-from-a-function\/\" data-type=\"post\" data-id=\"2071\">values <\/a>of the existing list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: Squaring numbers from 1 to 5 using list comprehension\nsquared_numbers = &#91;x**2 for x in range(1, 6)]\n\n# Output\nprint(squared_numbers)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 4, 9, 16, 25]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-list-comprehension-syntax\">What is Python List Comprehension Syntax?<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: newList = &#91; expression(element) for element in oldList if condition ] \n\n<strong>Parameter<\/strong><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Expression: It will represent the operation that you want to execute on every item within the iterable.<\/li>\n\n\n\n<li>element: The term &#8221;<a href=\"https:\/\/www.skillvertex.com\/blog\/how-to-print-a-variable-name-in-c\/\" data-type=\"post\" data-id=\"3299\">variable<\/a>&#8221; will indicate each value taken from the iterable.<\/li>\n\n\n\n<li>iterable: it will specify the sequence of the elements that are required to iterate through<\/li>\n\n\n\n<li>condition: A filter that will help to decide if the element should be added to the new list.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Return: The Return value of the list comprehension will be referred to as the new list that has the modified elements and will satisfy the given criteria.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"list-comprehension-in-python-example\">List Comprehension in Python Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The example below shows how to list comprehension to find the square of the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-numbers\/\" data-type=\"post\" data-id=\"7023\">number <\/a>in Python.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: List comprehension to square even numbers from a given list\ninput_numbers = &#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n\nsquared_even_numbers = &#91;x**2 for x in input_numbers if x % 2 == 0]\n\n# Output\nprint(squared_even_numbers)\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"> Output<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;4, 16, 36, 64, 100]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-example-of-iteration-with-list-comprehension\">What is the <strong>Example of Iteration with List Comprehension <\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/www.skillvertex.com\/blog\/mern-stack-website-example\/\" data-type=\"post\" data-id=\"4269\">example<\/a> provided below illustrates the iteration with the list Comprehension.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: Iterate over a list and add 10 to each element using list comprehension\noriginal_list = &#91;1, 2, 3, 4, 5]\n\nmodified_list = &#91;x + 10 for x in original_list]\n\n# Output\nprint(modified_list)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;11, 12, 13, 14, 15]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-an-example-of-an-even-list-using-list-comprehension\">What is an example of an Even List Using List Comprehension?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: Create a list of even numbers from 0 to 10 using list comprehension\neven_numbers = &#91;x for x in range(11) if x % 2 == 0]\n\n# Output\nprint(even_numbers)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;0, 2, 4, 6, 8, 10]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-matrix-using-the-list-comprehension\">What is Matrix using the List Comprehension?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The example provided below will show the matrix using the list comprehension.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: Create a 3x3 matrix and square each element using list comprehension\nmatrix = &#91;&#91;1, 2, 3],\n          &#91;4, 5, 6],\n          &#91;7, 8, 9]]\n\nsquared_matrix = &#91;&#91;x**2 for x in row] for row in matrix]\n\n# Output\nfor row in squared_matrix:\n    print(row)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 4, 9]\n&#91;16, 25, 36]\n&#91;49, 64, 81]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-difference-between-list-comprehension-vs-for-loop\">What is the difference between List Comprehension vs For Loop?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There are several ways to iterate through the list. So, the common way is to use the for <a href=\"https:\/\/www.skillvertex.com\/blog\/python-loop-lists\/\" data-type=\"post\" data-id=\"8077\">loop<\/a>. Check out the example given below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Empty list \nList = &#91;] \n  \n# Traditional approach of iterating \nfor character in 'Skill  vertex!: \n    List.append(character) \n  \n# Display list \nprint(List) <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'S' , 'k', 'i', 'l', 'l', 'v', 'e', 'r', 't', 'e', 'x','!']<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example above will show the implementation of the traditional approach inorder to iterate through the list such as list, <a href=\"https:\/\/www.skillvertex.com\/blog\/python-string-methods\/\" data-type=\"post\" data-id=\"7928\">string<\/a>, tuple, etc. Thus, the list comprehension will perform the same task and will make the program more simple.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, the list comprehension will translate the traditional iteration approach with the help of a for loop and turn it into a simple formula for easy use. So, the approach given below is used to iterate through the list, string, and tuple with the help of list comprehension in Python.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Using list comprehension to iterate through loop \nList = &#91;character for character in 'Skillvertex!'] \n  \n# Displaying list \nprint(List) <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'S', 'k', 'i','l', 'l', 'v', 'e', 'r', 't', 'e', 'x',  '!' ]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-time-analysis-in-list-comprehensions-and-loops\">What is Time Analysis in List Comprehensions and Loops?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The list comprehension in Python will be more efficient both computationally to the coding space and time than the loop. Hence, they will be written in a single line of code. So, the program given below will show the difference between loops and list comprehension depending on the performance.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\n\n# Time analysis for List Comprehension\nstart_time = time.time()\n\nsquared_numbers_comprehension = &#91;x**2 for x in range(1, 10**6)]\n\nend_time = time.time()\ncomprehension_time = end_time - start_time\n\n# Time analysis for Loop\nstart_time = time.time()\n\nsquared_numbers_loop = &#91;]\nfor x in range(1, 10**6):\n    squared_numbers_loop.append(x**2)\n\nend_time = time.time()\nloop_time = end_time - start_time\n\n# Output time analysis results\nprint(\"List Comprehension Time:\", comprehension_time, \"seconds\")\nprint(\"Loop Time:\", loop_time, \"seconds\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List Comprehension Time: 0.09034180641174316 seconds\nLoop Time: 0.1588280200958252 seconds\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-nested-list-comprehension\">What is Nested List Comprehension?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Nested List Comprehension is referred to as the list comprehension within the other list comprehension and works similarly to the nested for loops. The program given below shows the nested loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: Nested list comprehension to create a 3x3 matrix with squared elements\nmatrix_size = 3\nsquared_matrix = &#91;&#91;x**2 for x in range(row * matrix_size + 1, (row + 1) * matrix_size + 1)] for row in range(matrix_size)]\n\n# Output\nfor row in squared_matrix:\n    print(row)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 4, 9]\n&#91;16, 25, 36]\n&#91;49, 64, 81]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-list-comprehensions-and-lambda\">What is List Comprehensions and Lambda<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Lambda Expressions are referred to as the shorthand representations of the Python functions. With the help of list comprehensions with lambda, it will make an efficient combination.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: List comprehension with lambda function to square each element\noriginal_list = &#91;1, 2, 3, 4, 5]\n\nsquared_list = &#91;(lambda x: x**2)(x) for x in original_list]\n\n# Output\nprint(squared_list)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 4, 9, 16, 25]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conditionals-in-the-list-comprehension\">Conditionals in the List Comprehension<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This will add the conditions statements to the list comprehension. So, it will make a list using the range, operators, etc, and will apply some conditions to the list with the help of if statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-example-of-python-list-comprehension-using-the-if-else\">What is the Example of Python List Comprehension using the if-else<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check out the example to learn more about Python List Comprehension using the if-else.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: List comprehension with if-else to square even and cube odd numbers\noriginal_list = &#91;1, 2, 3, 4, 5]\n\nmodified_list = &#91;x**2 if x % 2 == 0 else x**3 for x in original_list]\n\n# Output\nprint(modified_list)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 4, 27, 16, 125]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-example-of-nested-if-with-the-list-comprehension\">What is the Example of Nested  IF  with the List Comprehension?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Example: Nested if in list comprehension to filter even numbers greater than 2\noriginal_list = &#91;1, 2, 3, 4, 5, 6, 7, 8, 9]\n\nfiltered_list = &#91;x for x in original_list if x &gt; 2 if x % 2 == 0]\n\n# Output\nprint(filtered_list)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;4, 6, 8]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"display-the-square-of-numbers-from-1-to-10\">Display the square of numbers  from 1 to 10<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check out the example below to display the square of numbers from 1 to 10.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Using a for loop to display the square of numbers from 1 to 10\nfor num in range(1, 11):\n    square = num ** 2\n    print(f\"The square of {num} is: {square}\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>The square of 1 is: 1\nThe square of 2 is: 4\nThe square of 3 is: 9\nThe square of 4 is: 16\nThe square of 5 is: 25\nThe square of 6 is: 36\nThe square of 7 is: 49\nThe square of 8 is: 64\nThe square of 9 is: 81\nThe square of 10 is: 100\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-example-to-display-transpose-of-2-d-matrix\">What is the Example to  Display Transpose of 2D- Matrix?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to calculate the transpose of a matrix\ndef transpose(matrix):\n    # Using zip() to transpose the matrix\n    transposed_matrix = &#91;list(row) for row in zip(*matrix)]\n    return transposed_matrix\n\n# Example 2D matrix\nmatrix = &#91;\n    &#91;1, 2, 3],\n    &#91;4, 5, 6],\n    &#91;7, 8, 9]\n]\n\n# Display original matrix\nprint(\"Original Matrix:\")\nfor row in matrix:\n    print(row)\n\n# Display transpose of the matrix\ntransposed_matrix = transpose(matrix)\nprint(\"\\nTransposed Matrix:\")\nfor row in transposed_matrix:\n    print(row)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original Matrix:\n&#91;1, 2, 3]\n&#91;4, 5, 6]\n&#91;7, 8, 9]\n\nTransposed Matrix:\n&#91;1, 4, 7]\n&#91;2, 5, 8]\n&#91;3, 6, 9]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toggle-the-case-of-each-character-in-the-string\">Toggle the case of each character in the string<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to toggle the case of each character in a string\ndef toggle_case(input_string):\n    toggled_string = ''.join(&#91;char.lower() if char.isupper() else char.upper() for char in input_string])\n    return toggled_string\n\n# Example string\ninput_string = \"Hello World!\"\n\n# Display original string\nprint(\"Original String:\", input_string)\n\n# Toggle the case of each character\ntoggled_string = toggle_case(input_string)\n\n# Display the result\nprint(\"Toggled String:\", toggled_string)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original String: Hello World!\nToggled String: hELLO wORLD!\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-example-to-reverse-each-string-in-a-tuple\">What is the example to reverse each string in a Tuple?<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to reverse each string in a tuple\ndef reverse_strings_in_tuple(input_tuple):\n    reversed_tuple = tuple(s&#91;::-1] for s in input_tuple)\n    return reversed_tuple\n\n# Example tuple of strings\noriginal_tuple = (\"hello\", \"world\", \"python\", \"code\")\n\n# Display original tuple\nprint(\"Original Tuple:\", original_tuple)\n\n# Reverse each string in the tuple\nreversed_tuple = reverse_strings_in_tuple(original_tuple)\n\n# Display the result\nprint(\"Reversed Tuple:\", reversed_tuple)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original Tuple: ('hello', 'world', 'python', 'code')\nReversed Tuple: ('olleh', 'dlrow', 'nohtyp', 'edoc')\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-example-of-creating-the-list-of-tuples-from-the-two-separate-lists\">What is the Example of creating the list of Tuples from the two separate Lists?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let us look into the example given below to create the two lists of names and ages with the help of zip() in the list comprehension so, it is possible to insert the name and age as the tuple to the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-add-list-items\/\" data-type=\"post\" data-id=\"8058\">list<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to create a list of tuples from two separate lists\ndef create_tuples(list1, list2):\n    tuple_list = list(zip(list1, list2))\n    return tuple_list\n\n# Example lists\nlist1 = &#91;1, 2, 3, 4]\nlist2 = &#91;'a', 'b', 'c', 'd']\n\n# Display original lists\nprint(\"List 1:\", list1)\nprint(\"List 2:\", list2)\n\n# Create a list of tuples from the two lists\ntuple_list = create_tuples(list1, list2)\n\n# Display the result\nprint(\"List of Tuples:\", tuple_list)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List 1: &#91;1, 2, 3, 4]\nList 2: &#91;'a', 'b', 'c', 'd']\nList of Tuples: &#91;(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-example-to-display-the-sum-of-digits-of-all-the-odd-elements-in-the-list\">What is the Example to display the sum of digits of all the odd elements in the list?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The example provided below has made a list and that will help us to find the digit sum of every odd <a href=\"https:\/\/www.skillvertex.com\/blog\/which-one-is-not-an-element-of-iot\/\" data-type=\"post\" data-id=\"3089\">element <\/a>in the list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to calculate the sum of digits in a number\ndef sum_of_digits(number):\n    return sum(int(digit) for digit in str(abs(number)))\n\n# Function to calculate the sum of digits for odd elements in the list\ndef sum_of_digits_for_odd_elements(input_list):\n    odd_elements = &#91;element for element in input_list if element % 2 != 0]\n    sum_of_digits_odd = sum(sum_of_digits(element) for element in odd_elements)\n    return sum_of_digits_odd\n\n# Example list of numbers\nnumber_list = &#91;123, 45, 678, 91, 234]\n\n# Display original list\nprint(\"Original List:\", number_list)\n\n# Calculate the sum of digits for odd elements in the list\nresult = sum_of_digits_for_odd_elements(number_list)\n\n# Display the result\nprint(\"Sum of digits for odd elements:\", result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original List: &#91;123, 45, 678, 91, 234]\nSum of digits for odd elements: 21\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In conclusion, Python List Comprehension is a concise and powerful feature that allows for the creation of lists in a more compact and readable manner. It provides a succinct syntax to generate lists, filter elements, and apply expressions in a single line of code. List comprehensions enhance code readability, reduce the need for explicit loops, and contribute to more expressive and Pythonic programming. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Moreover, they are particularly useful for tasks involving iteration, filtering, and transformation of data, making code both efficient and elegant. Overall, Python List Comprehension is a valuable tool for simplifying list-related operations and improving code efficiency.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-list-comprehension-fa-qs\">Python List Comprehension- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1710322590717\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.What is list comprehension in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The list comprehension is very easy to read, compact, and has an elegant way of forming the list from the existing iterable object.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710322596819\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.What are the 4 types of comprehension in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. List comprehension, dictionary comprehension, set comprehension, and generator comprehension are the 4 types of comprehension in Python.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710322603878\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is list comprehension in Python in range?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  It is a concise syntax that is used to create the list from the range or an iterable object by applying the specified object on each of its items.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The Python list comprehension involves a bracket with the expression and will be executed for each element and the for loop to iterate over each element in the Python list. Read this article to learn more about Python List Comprehension. What is Python List Comprehension? The List Comprehension will provide the shorter syntax and will &#8230; <a title=\"Python List Comprehension\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-list-comprehension\/\" aria-label=\"More on Python List Comprehension\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":8096,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[932,964,972,866],"class_list":["post-8090","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python-functions","tag-python-list","tag-python-list-comprehension","tag-python-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=8090"}],"version-history":[{"count":8,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8090\/revisions"}],"predecessor-version":[{"id":8295,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8090\/revisions\/8295"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/8096"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=8090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=8090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=8090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}