PYTHON - List Exercises
Python - List Exercises
Python List Exercise 1
Python program to find unique numbers in a given list.
PYTHON CODE
L1 = [1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2]
L2 = []
for x in L1:
if x not in L2:
L2.append(x)
print (L2)It will produce the followingoutput−
PYTHON CODE
[1, 9, 6, 3, 4, 5, 2, 7, 8]Python List Exercise 2
Python program to find sum of all numbers in a list.
PYTHON CODE
L1 = [1, 9, 1, 6, 3, 4]
ttl = 0
for x in L1:
ttl+=x
print ("Sum of all numbers Using loop:", ttl)
ttl = sum(L1)
print ("Sum of all numbers sum() function:", ttl)It will produce the followingoutput−
PYTHON CODE
Sum of all numbers Using loop: 24
Sum of all numbers sum() function: 24Python List Exercise 3
Python program to create a list of 5 random integers.
PYTHON CODE
import random
L1 = []
for i in range(5):
x = random.randint(0, 100)
L1.append(x)
print (L1)It will produce the followingoutput−
PYTHON CODE
[77, 3, 20, 91, 85]More List Exercise Programs
<ul class="list">
<li>pPython program to remove all odd numbers from a list.</li>
<li>pPython program to sort a list of strings on the number of alphabets in each word.</li>
<li>pPython program non-numeric items in a list in a separate list.</li>
<li>pPython program to create a list of integers representing each character in a string</li>
<li>pPython program to find numbers common in two lists.</li>
</ul>