w3resource

Python: Extract every first or specified element from a given two-dimensional list

Python List: Exercise - 144 with Solution

Write a Python program to extract every first or specified element from a given two-dimensional list.

Sample Solution:

Python Code:

# Define a function 'specified_element' that extracts elements at a specified index from a list of lists
def specified_element(nums, N):
    # Use a list comprehension to extract the element at index 'N' from each sub-list
    result = [i[N] for i in nums]
    return result

# Create a list of lists 'nums' representing a 2D matrix
nums = [
        [1, 2, 3, 2],
        [4, 5, 6, 2],
        [7, 1, 9, 5],
       ]

# Print a message indicating the original list of lists
print("Original list of lists:")
# Print the contents of 'nums'
print(nums)

# Set the index 'N' to 0
N = 0
# Print a message indicating the operation to extract every first element
print("\nExtract every first element from the said given two-dimensional list:")
# Call the 'specified_element' function with 'nums' and index 'N', then print the result
print(specified_element(nums, N))

# Set the index 'N' to 2
N = 2
# Print a message indicating the operation to extract every third element
print("\nExtract every third element from the said given two-dimensional list:")
# Call the 'specified_element' function with 'nums' and index 'N', then print the result
print(specified_element(nums, N)) 

Sample Output:

Original list of lists:
[[1, 2, 3, 2], [4, 5, 6, 2], [7, 1, 9, 5]]

Extract every first element from the said given two dimensional list:
[1, 4, 7]

Extract every third element from the said given two dimensional list:
[3, 6, 9]

Flowchart:

Flowchart: Extract every first or specified element from a given two-dimensional list.

Python Code Editor:

Previous: Write a Python program to get the frequency of the elements in a given list of lists.
Next: Write a Python program to generate a number in a specified range except some specific numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/list/python-data-type-list-exercise-144.php