Some Basic Python List Programs-2


Python program to copy a list

list1 = [9, 4, 10, 5]
list2 = list(list1)
print("Original list: ",list1)
print("Copied list: ",list2)
#Output: Original list:  [9, 4, 10, 5]
         Copied list:  [9, 4, 10, 5]
  • The list() built-in method takes an iterable and creates a new list of its elements.

Python program to concatenate multiple lists

list1 = [12, 7, 9] 
list2 = [16, 2, 15, 11]
list3 = [13, 4, 18]

#using + operator
con_list1 = list1 + list2 + list3
print(con_list1)
#Output: [12, 7, 9, 16, 2, 15, 11, 13, 4, 18]

#using + operator
con_list2 = [*list1, *list2, *list3]
print(con_list2)
#Output: [12, 7, 9, 16, 2, 15, 11, 13, 4, 18]

#using list comprehension
con_list3 = [ele for list in [list1, list2, list3] for ele in list]
print(con_list3)
#Output: [12, 7, 9, 16, 2, 15, 11, 13, 4, 18]
  • The ‘+’ operator joins multiple lists together into one list.
  • The ‘*’ operator unpacks every element of an iterable to other variables dynamically.
  • List comprehension is a compact method that defines a list and its contents at the same time. The basic syntax of list comprehension consists of a square bracket with an expression that is executed for each element along with for loop to iterate over each element. Read more: List Comprehension in Python – Explained with Examples

Python program to find common elements in multiple lists

list1 = [15, 4, 7] 
list2 = [11, 2, 15, 7]
list3 = [1, 15, 4, 6, 19]
list4 = [5, 15]
print(set(list1) & set(list2) & set(list3) & set(list4))
#Output: {15}
  • A set in python represents a mathematical notion of a set and performs set operations like union, intersection, difference, etc. It is an unordered collection of immutable and unique elements. The ‘&’ operator executes the intersection between sets. Read more: Set Data Type in Python

Python program to find the difference between the two lists

list1 = [6, 16, 1, 4, 13]
list2 = [18, 4, 16, 2] 

#using list comprehension
diff_list1 = [i for i in list1 + list2 if i not in list1 or i not in list2] 
print(diff_list1)
#Output: [6, 1, 13, 18, 2]

#using ^ operator
diff_list2 = list(set(list1) ^ set(list2))
print(diff_list2) 
#Output: [1, 2, 6, 13, 18]

#using set symmetric_difference() method 
diff_list3 = list(set(list1).symmetric_difference(set(list2))) 
print(diff_list3) 
#Output: [1, 2, 6, 13, 18]
  • The symmetric difference between two sets means a set of elements belonging to each set but not belonging to both of the sets. It is performed by using a symmetric_difference() built-in set method or ^ operator in Python.

Python program to iterate over multiple lists simultaneously

list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c', 'd']
list3 = ['A', 'B', 'C', 'D']
list4 = ['I', 'II', 'III', 'IV']
for (ele1, ele2, ele3, ele4) in zip(list1, list2, list3, list4):
    print(ele1, ele2, ele3, ele4)
#Output: 1 a A I
         2 b B II
         3 c C III
         4 d D IV
  • The zip() built-in method returns an iterator of tuples, by grouping elements of the similar index of multiple sequences so that they can be used as a single entity.
  • Tuple unpacking allows extracting tuple elements automatically by assigning each value on the right-hand side to its respective variable on the left side.

Python program to get the frequency of the elements in a list

from collections import Counter 
my_list = [1, 1, 2, 2, 1, 1, 2, 5, 5, 5, 4, 3, 4, 3, 3, 2, 4, 1, 3]
frequency = dict(Counter(my_list))
print(frequency)
#Output: {1: 5, 2: 4, 5: 3, 4: 3, 3: 4}
  • The Collections module implements high-performance container datatypes and contains many useful data containers.
  • The Counter is a subclass of ‘dict’ used for counting iterable items. It is a container included in the collections module where elements of an iterable are stored as dictionary keys and their frequencies in the iterable are stored as dictionary values. Counter elements count can be positive, zero, or negative integers.
  • The dict() built-in method creates a dictionary of an iterable. A dictionary is an unordered collection of mutable values indexed by unique keys. Read more: Dictionary and Tuple in Python – Difference in Properties

Related Posts-

Leave a Comment

%d bloggers like this: