Question 27: Write a Python program to find common items from two lists. Download hole Program / Project code, by clicking following link: When to used list in Python ? In Python, a list is a commonly used data structure that allows you to store and manipulate a collection of items. Lists are versatile and can be used in various situations. Here are some common scenarios in which you would use a list: - Storing Multiple Values: Lists are used to store multiple values of any data type, including integers, strings, floats, and even other lists. For example, you can use a list to store a list of numbers, names, or any other type of data.
numbers = [1, 2, 3, 4, 5]
names = ["Alice", "Bob", "Charlie"]
- Iterating Through Elements: Lists are iterable, which means you can easily loop through their elements using loops like for or while. This makes it convenient to process a collection of items.
for number in numbers:
print(number) - Accessing Elements: Lists allow you to access elements by their index. The List index starts from 0 for the first element. This feature is useful when you need to work with specific items in the collection.
first_name = names[0] # Access the first name
- Modifying Elements: Lists are mutable, meaning you can change their elements. You can update, add, or remove items from a list.
numbers[0] = 10 # Update the first number
names.append("David") # Add a new name to the list
names.remove("Bob") # Remove a name from the list
- Sorting and Searching: Lists provide methods for sorting elements in ascending or descending order (sort()) and for searching for specific elements (index() or in operator).
numbers.sort() # Sort the list in ascending order
index = names.index("Charlie") # Find the index of "Charlie"
- Stacks and Queues: Lists can be used to implement data structures like stacks and queues by using the append() and pop() methods.
# Implementing a stack
stack = []
stack.append(1) # Push an item onto the stack
popped_item = stack.pop() # Pop an item from the stack
# Implementing a queue
queue = []
queue.append(1) # Enqueue an item
dequeued_item = queue.pop(0) # Dequeue an item
- Storing Data for Analysis: Lists are often used to store data that needs to be analyzed or processed further, such as data from a file or user input.
- Dynamic Collections: Lists can change in size as needed. You can add or remove elements as your program runs, making them suitable for situations where the size of the collection is not fixed.
In summary, lists are a fundamental data structure in Python that you can use to store, manipulate, and work with collections of items in various programming scenarios. Programming Code: Following code write in: BP_P27.py # Python Program to find common items from two lists
# using intersection function
# make two lists
def common_items(a, b):
a = set(a)
b = set(b)
# check length
if len(a.intersection(b)) > 0:
return("List 1 & 2 common items: ", a.intersection(b))
else:
return("List 1 & 2 having no common items.")
# a = [10, 20, 30, 40, 50]
# b = [20, 40, 60, 80, 100]
# common items of given input lists
num = int(input("Enter number of List 1 integer items: "))
a = []
b = []
for i in range(0, num):
c = int(input(f"Enter List 1 integer item {i}: "))
a.append(c)
print("List 1: ", a)
for i in range(0, num):
c = int(input(f"Enter List 2 integer item {i}: "))
b.append(c)
print("List 2: ", b)
print(common_items(a, b))
# you can also define separately num for b list
# Thanks for Reading.
Output:
When to used list in Python ?
In Python, a list is a commonly used data structure that allows you to store and manipulate a collection of items. Lists are versatile and can be used in various situations. Here are some common scenarios in which you would use a list:
- Storing Multiple Values: Lists are used to store multiple values of any data type, including integers, strings, floats, and even other lists. For example, you can use a list to store a list of numbers, names, or any other type of data.
numbers = [1, 2, 3, 4, 5]
names = ["Alice", "Bob", "Charlie"] - Iterating Through Elements: Lists are iterable, which means you can easily loop through their elements using loops like for or while. This makes it convenient to process a collection of items.
for number in numbers:
print(number) - Accessing Elements: Lists allow you to access elements by their index. The List index starts from 0 for the first element. This feature is useful when you need to work with specific items in the collection.
first_name = names[0] # Access the first name - Modifying Elements: Lists are mutable, meaning you can change their elements. You can update, add, or remove items from a list.
numbers[0] = 10 # Update the first number
names.append("David") # Add a new name to the list
names.remove("Bob") # Remove a name from the list - Sorting and Searching: Lists provide methods for sorting elements in ascending or descending order (sort()) and for searching for specific elements (index() or in operator).
numbers.sort() # Sort the list in ascending order
index = names.index("Charlie") # Find the index of "Charlie" - Stacks and Queues: Lists can be used to implement data structures like stacks and queues by using the append() and pop() methods.
# Implementing a stack
stack = []
stack.append(1) # Push an item onto the stack
popped_item = stack.pop() # Pop an item from the stack
# Implementing a queue
queue = []
queue.append(1) # Enqueue an item
dequeued_item = queue.pop(0) # Dequeue an item - Storing Data for Analysis: Lists are often used to store data that needs to be analyzed or processed further, such as data from a file or user input.
- Dynamic Collections: Lists can change in size as needed. You can add or remove elements as your program runs, making them suitable for situations where the size of the collection is not fixed.
# Python Program to find common items from two lists
# using intersection function
# make two lists
def common_items(a, b):
a = set(a)
b = set(b)
# check length
if len(a.intersection(b)) > 0:
return("List 1 & 2 common items: ", a.intersection(b))
else:
return("List 1 & 2 having no common items.")
# a = [10, 20, 30, 40, 50]
# b = [20, 40, 60, 80, 100]
# common items of given input lists
num = int(input("Enter number of List 1 integer items: "))
a = []
b = []
for i in range(0, num):
c = int(input(f"Enter List 1 integer item {i}: "))
a.append(c)
print("List 1: ", a)
for i in range(0, num):
c = int(input(f"Enter List 2 integer item {i}: "))
b.append(c)
print("List 2: ", b)
print(common_items(a, b))
# you can also define separately num for b list
# Thanks for Reading.
Output:
0 Comments