#PYTHON(DAY45)
Example Programs
1) Python Program to Flatten a Nested List
my_list = [[1], [2, 3], [4, 5, 6, 7]]
flat_list = []
for sublist in my_list:
for num in sublist:
flat_list.append(num)
print(flat_list)
And the output is:
[1, 2, 3, 4, 5, 6, 7]
2) Python Program to Slice Lists
my_list = [1, 2, 3, 4, 5]
print(my_list[:])
And the output is:
[1, 2, 3, 4, 5]
3) Access both key and value using items()
dt = {'a': 'juice', 'b': 'grill', 'c': 'corn'}
for key, value in dt.items():
print(key, value)
And the output is:
a juice
b grill
c corn
No comments:
Post a Comment