How to get every first element in 2 dimensional list in python

How to get every first element in 2 dimensional list in python

To extract every first element from a 2-dimensional list (list of lists) in Python, you can use a list comprehension or a loop. Here's how you can do it:

Using a List Comprehension:

# Example 2-dimensional list two_dimensional_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Extract the first element from each sublist using a list comprehension first_elements = [sublist[0] for sublist in two_dimensional_list] print(first_elements) # Output: [1, 4, 7] 

Using a Loop:

# Example 2-dimensional list two_dimensional_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Extract the first element from each sublist using a loop first_elements = [] for sublist in two_dimensional_list: first_elements.append(sublist[0]) print(first_elements) # Output: [1, 4, 7] 

In both examples, we iterate through the sublists in the 2-dimensional list and extract the first element from each sublist, appending it to the first_elements list.

Choose the approach that suits your coding style and requirements.

Examples

  1. Python: Get every first element from a 2D list using list comprehension:

    Description: This code snippet demonstrates how to extract every first element from each sublist of a 2D list using list comprehension.

    # Original 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Extract every first element first_elements = [sublist[0] for sublist in matrix] print(first_elements) 
  2. Python: Retrieve the first element from each sublist of a 2D list using a loop:

    Description: This code showcases how to iterate through each sublist of a 2D list and extract the first element using a loop.

    # Original 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Initialize an empty list to store first elements first_elements = [] # Iterate through each sublist for sublist in matrix: first_elements.append(sublist[0]) print(first_elements) 
  3. Python: Extract the first element from each sublist of a 2D list using map() function:

    Description: This code demonstrates how to use the map() function to extract the first element from each sublist of a 2D list.

    # Original 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Extract first elements using map() function first_elements = list(map(lambda x: x[0], matrix)) print(first_elements) 
  4. Python: Access every first element from a 2D list using slicing:

    Description: This code snippet illustrates how to access every first element from each sublist of a 2D list using slicing.

    # Original 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Extract first elements using slicing first_elements = [sublist[:1][0] for sublist in matrix] print(first_elements) 
  5. Python: Get the first element from each sublist of a 2D list using zip() function:

    Description: This code showcases how to use the zip() function to extract the first element from each sublist of a 2D list.

    # Original 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Extract first elements using zip() function first_elements = [sublist[0] for sublist in zip(*matrix)] print(first_elements) 
  6. Python: Get every first element from a 2D list using itemgetter() function from the operator module:

    Description: This code demonstrates how to use the itemgetter() function from the operator module to extract every first element from each sublist of a 2D list.

    from operator import itemgetter # Original 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Extract first elements using itemgetter() function first_elements = list(map(itemgetter(0), matrix)) print(first_elements) 
  7. Python: Extract the first element from each sublist of a 2D list using a list comprehension with unpacking:

    Description: This code snippet demonstrates how to use a list comprehension with unpacking to extract the first element from each sublist of a 2D list.

    # Original 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Extract first elements using list comprehension with unpacking first_elements = [first for first, *_ in matrix] print(first_elements) 
  8. Python: Get the first element from each sublist of a 2D list using a loop with unpacking:

    Description: This code showcases how to iterate through each sublist of a 2D list and extract the first element using a loop with unpacking.

    # Original 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Initialize an empty list to store first elements first_elements = [] # Iterate through each sublist with unpacking for first, *_ in matrix: first_elements.append(first) print(first_elements) 
  9. Python: Access every first element from a 2D list using numpy:

    Description: This code snippet illustrates how to use NumPy to access every first element from each sublist of a 2D list.

    import numpy as np # Original 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Convert the list to a NumPy array array = np.array(matrix) # Extract first elements using numpy indexing first_elements = array[:, 0] print(first_elements) 
  10. Python: Retrieve the first element from each sublist of a 2D list using a list comprehension with indexing:

    Description: This code demonstrates how to use a list comprehension with indexing to extract the first element from each sublist of a 2D list.

    # Original 2D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Extract first elements using list comprehension with indexing first_elements = [sublist[0] for sublist in matrix] print(first_elements) 

More Tags

posts frames ef-core-2.2 dead-reckoning timepicker associations react-boilerplate uploadify tlist npoi

More Python Questions

More Livestock Calculators

More Dog Calculators

More Fitness Calculators

More Animal pregnancy Calculators