w3resource

Python: Common index elements from more than one list

Python List: Exercise - 100 with Solution

Write a Python program to extract common index elements from more than one given list.

Sample Solution:

Python Code:

def extract_index_ele(l1, l2, l3):
    result = []
    for m, n, o in zip(l1, l2, l3):
        if (m == n == o):
            result.append(m)
    return result

nums1 = [1, 1, 3, 4, 5, 6, 7]
nums2 = [0, 1, 2, 3, 4, 5, 7]
nums3 = [0, 1, 2, 3, 4, 5, 7]

print("Original lists:")
print(nums1)
print(nums2)
print(nums3)
print("\nCommon index elements of the said lists:") 
print(extract_index_ele(nums1, nums2, nums3))

Sample Output:

Original lists:
[1, 1, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 7]
[0, 1, 2, 3, 4, 5, 7]

Common index elements of the said lists:
[1, 7]

Pictorial Presentation:

Python List: Common index elements from more than one list.

Flowchart:

Flowchart: Common index elements from more than one list.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the maximum and minimum values in a given heterogeneous list.
Next: Write a Python program to sort a given matrix in ascending order according to the sum of its rows.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Given a predicate function, fn, and a prop string, this curried function will then take an object to inspect by calling the property and passing it to the predicate:

Example:

def tips_check_prop(fn, prop):
  return lambda obj: fn(obj[prop])
check_age = tips_check_prop(lambda x: x >= 25, 'age')
user = {'name': 'Owen', 'age': 25}

print(check_age(user))

Output:

True 

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook