Python List Membership Test Using “in” and “not in”
We can test if an item exists in a list or not, using the keyword “in” or “not in”.
n1=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(n1)
print(‘c’ in n1)
print(‘o’ in n1)
print(‘x’ not in n1)
print(‘p’ not in n1)
#Output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
True
True
True
False
n=”computer”
print(n)
print(‘c’ in n)
print(‘o’ in n)
print(‘x’ not in n)
print(‘p’ not in n)
#Output:
computer
True
True
True
False
Iterating Through a List
Using a for loop we can iterate through each item in a list.
Example:
n=[1,2,3,4,5] for i in n: print(i)
Output:
1
2
3
4
5
Example:
n=["amit","sumit","kapil","mukesh"] for i in n: print(i)
Output:
amit
sumit
kapil
mukesh
Return value from list()
The list() constructor returns a mutable sequence list of elements.
• If no parameters are passed, it creates an empty list
• If text is passed as parameter, it creates a list of elements in the text
print(list())
#creates an empty list
vowels1 = ‘aeiou’
print(vowels1)
#displays as text
vowels2 = ‘aeiou’
print(list(vowels2))
#displays as list
vowels3 = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
print(vowels3)
#displays as list
#Output:
[]
aeiou
[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
List Comprehension: Elegant way to create new List
List comprehension is an elegant and concise way to create new list from an existing list in Python.
List comprehension consists of an expression followed by for statement inside square brackets.
Example:1
To make a list of all even number upto 20
n=[x for x in range(1,21) if(x%2==0)] print(n)
Output:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Example:2
To make a list of all odd number upto 20
n=[x for x in range(1,21) if(x%2==1)] print(n)
Output:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Example:3
To make a list of all number multiple of 5 upto 100
n=[x for x in range(1,101) if(x%5==0)] print(n)
Output:
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
Example:4
To make a list with each item being increasing power of 2.
n = [2 ** x for x in range(10)] print(n)
Output:
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
List Concatenation Operator (+)
We can also use + operator to combine two lists. This is also called concatenation.
Example:11
n = [1, 3, 5]
print(n + [9, 7, 5])
Output:
[1, 3, 5, 9, 7, 5]
Example:12
n=[1,2,3,4,5]
n1=[6,7,8,9,10]
print(“list n”)
print(n)
print(“list n1”)
print(n1)
print(“n+n1”)
print(n+n1)
print(n+[10,20,30])
Output:
list n
[1, 2, 3, 4, 5]
list n1
[6, 7, 8, 9, 10]
n+n1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 10, 20, 30]
For Practice
Question :
n1=[1,2,3,4,5]
n2=[6,7,8,9,10]
print(n1)
print(n2)
print(n1+n2)
n=n1+n2
print(n1)
print(n2)
print(n)
n=n2+n1
print(n)
output:
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
Question:
n1=[1,2,3,4,5]
n2=[6,7,8,9,10]
print(n1)
print(n2)
n1=n1+[100,200,300]
print(n1)
n2=n1+n2
print(n2)
n2=n2+[23,45,56]+n1
print(n2)
Output:
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 100, 200, 300]
[1, 2, 3, 4, 5, 100, 200, 300, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 100, 200, 300, 6, 7, 8, 9, 10, 23, 45, 56, 1, 2, 3, 4, 5, 100, 200, 300]