0% found this document useful (0 votes)
4 views3 pages

Python Programming Exercises Session 2

Uploaded by

yusuf01asif
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python Programming Exercises Session 2

Uploaded by

yusuf01asif
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

5/3/2018 Let's Do Together - Session 2

In [4]:

#Write a Python program to check if all dictionaries in a list are empty or not.

In [9]:

#solution
my_list1 = [{},{},{}]
my_list2 = [{123,2345},{},{}]
print(all(not d for d in my_list1))
print(all(not d for d in my_list2))

True
False

In [6]:

#Write a Python program to remove duplicates from a list of lists.

In [8]:

#Solution
import itertools
num = [[110, 120], [240], [330, 456, 425], [310, 220], [133], [240]]
print("Original List", num)
[Link]()
new_num = list(num for num,_ in [Link](num))
print("New List", new_num)

Original List [[110, 120], [240], [330, 456, 425], [310, 220], [133], [24
0]]
New List [[110, 120], [133], [240], [310, 220], [330, 456, 425]]

In [10]:

#Write a Python program to extend a list without append.

In [11]:

#solution
x = [103, 320, 430]
y = [403, 503, 603]
x[:0] =y
print(x)

[403, 503, 603, 103, 320, 430]

In [12]:

#Write a Python program to find the list in a list of lists whose sum of elements is th
e highest

[Link]
5/3/2018 Let's Do Together - Session 2

In [13]:

#solution
num = [[1,2,3], [4,5,6], [10,11,12], [7,8,9]]
print(max(num, key=sum))

[10, 11, 12]

In [14]:

#Write a Python program to access dictionary key’s element by index.

In [16]:

#solution
num = {'stats': 80, 'math': 90, 'algorithm': 86}
print(list(num)[0])

stats

In [17]:

#Write a Python program to iterate over two lists simultaneously.


num = [1, 2, 3]
color = ['red', 'while', 'black']
for (a,b) in zip(num, color):
print(a, b)

1 red
2 while
3 black

In [18]:

#write a program to inser a string at the begining of every elements in a list

In [22]:

#solution
a = [100,123,345,567,789,890,98,876,543,678]

#enter customer before each element


print(['customer{0}'.format(i) for i in a])

['customer100', 'customer123', 'customer345', 'customer567', 'customer78


9', 'customer890', 'customer98', 'customer876', 'customer543', 'customer67
8']

In [23]:

# write a program to take two lists and print if they have at least one common member

[Link]
5/3/2018 Let's Do Together - Session 2

In [24]:

#solution
def common_data(list1, list2):
result = False
for x in list1:
for y in list2:
if x == y:
result = True
return result
print(common_data([121,222,332,432,125], [125,236,457,678,779]))
print(common_data([1,2,3,4,5], [6,7,8,9]))

True
None

In [25]:

# compute all permutations in a list

In [26]:

#solution
import itertools
print(list([Link]([12,22,23])))

[(12, 22, 23), (12, 23, 22), (22, 12, 23), (22, 23, 12), (23, 12, 22), (2
3, 22, 12)]

In [ ]:

[Link]

You might also like