0% found this document useful (0 votes)
58 views4 pages

Implementing Real-Time - Technical Applications Using Lists

The document discusses various operations that can be performed on lists in Python like accessing elements, modifying lists, sorting, filtering, mapping and reducing lists. It also discusses list comprehensions and nested lists. Various examples and problems are provided to demonstrate working with lists.

Uploaded by

Vijay Udith
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)
58 views4 pages

Implementing Real-Time - Technical Applications Using Lists

The document discusses various operations that can be performed on lists in Python like accessing elements, modifying lists, sorting, filtering, mapping and reducing lists. It also discusses list comprehensions and nested lists. Various examples and problems are provided to demonstrate working with lists.

Uploaded by

Vijay Udith
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
You are on page 1/ 4

7.

Implementing real-time/technical applications using Lists

List
1. Assume that the variable ‘data’ refers to the list [5, 3, 7]. Write the values of the following
expressions:
a. data[2]
b. data[-1]
c. len(data)
d. data[0:2]
e. 0 in data
f. data + [2, 10, 5]

2. For lst = [4, 2, 9, 1], what is the result of each of the following list operations?
a. lst[1]
b. lst.insert(2, 3)
c. del lst[3]
d. lst.append(3)
e. Delete 1st and 2nd element in the list using slicing.

3. Assume that the variable’ data’ refers to the list [5, 3, 7]. Write the expressions that perform
the following tasks:
a. Replace the value at position 0 in data with that value’s negation.
b. Add the value 10 to the end of data.
c. Insert the value 22 at position 2 in data.
d. Remove the value at position 1 in data.
e. Add the values in the list newData=[9,11,13] to the end of data.
f. Locate the index of the value 7 in data, safely.
g. Find the maximum element in the list.
h. Find the sum of all elements in the list.
i. Sort the values in data.

4. Why it is necessary to have both the functions append() and extend()? What is the result of
the following expression that uses append where it probably intended to use extend()?

lst = [1, 2, 3]

lst.append([4, 5, 6])

5. Show how to use the ‘is’ operator to demonstrate that assignment creates a duplicate
reference, and not a true copy. Then use the same operator to demonstrate that a slice assignment
does create a copy.
6. For fruit = ['apple', 'banana', 'pear', 'cherry'], use a list operation to change the list to ['apple',
'banana', 'cherry']

7. For a list of integers, lst, give the code to retrieve the maximum value of the second half of the
list.

8. Write a program to print index at which particular value exists. If the value exists at multiple
locations in the list, then print all the indices. Also count the number of times that value is
repeated in the list

9. For a list of integers named nums,

a. Write a ‘while loop’ that adds up all the values in nums.


b. Write a ‘for loop’ that adds up all the values in nums in which the loop variable is
assigned each value in the list.
c. Write a ‘for loop’ that adds up all the elements in nums in which the loop variable
is assigned to the index value of each element in the list.
d. Write a ‘for loop’ that displays the elements in nums backwards.
e. Write a ‘for loop’ that displays every other element in nums, starting with the first
element.

10. Write a program to read a string from the user and convert to list and display the list.

11. Write a program to append a value to a list without using append () function [Hint: use
combination of lst.insert() and len()]

12. The function randint from the random module can be used to produce random numbers. A
call on random.randint(1, 6), for example, will produce the values 1 to 6 with equal probability.
Write a program that loops n times. On each iteration, it makes two calls on randint to simulate
rolling a pair of dice. Compute the sum of the two dice. After the loop, print the list of sums.

13. Write a Python program that prompts the user for a list of integers, stores in another list only
those values between 1 and 100, and displays the resulting list.

14. Write a Python program that prompts the user for a list of integers and stores them in a list.
For all values that are greater than 100, the string 'over' should be stored instead. The program
should display the resulting list.

15. Write a Python program that prompts the user to enter a list of first names and stores them in
a list. The program should display how many times the letter 'a' appears within the list.

16. Write a Python program that prompts the user to enter a list of words and stores in a list only
those words whose first letter occurs again within the word (for example, 'Baboon'). The
program should display the resulting list.
17. Write a Python program that prompts the user to enter integer values for each of two lists. It
then should displays whether the lists are of the same length, whether the elements in each list
sum to the same value, and whether there are any values that occur in both lists.

18. Write a Python program using reduce ( ) function to calculate the sum of first 20 even
numbers.

19. Write a Python program that uses filter ( ) function to filter out only even numbers from a
list.

20. Write a Python program that uses map ( ) to print the double value of each element in a list.

List Comprehension:

1. Create a list named ‘nums’ with the combination of both positive and negative numbers. Using
list comprehension create a list named ‘pnums’ that contains only positive numbers of list ‘nums’
and create another list named ‘nnums’ that contains only negative numbers of list ‘nums’

2. Create a list that that contains few words as members, using list comprehension create a list
that contains only the first letter of each word

3. Create 2 lists, lst1 and lst2. Using comprehension create a list that is sum of each of the
elements from the two lists.

4. Use list comprehension to construct the following lists

a. [‘1a’,’2a’,’3a’,’4a’]
b.[‘ab’,’ac’,’ad’,’bb’,’bc’,’bd’]
c. Multiples of 10 (n values)
5. Give an appropriate list comprehension for each of the following.
a. Producing a list of consonants that appear in string w.
b. Producing a list of numbers between 1 and 100 that are divisible by 3
c. Producing a list of numbers, zero_values from a list of floating-point values, data_values,
that is within some distance, epsilon, from 0.

Nested List:

1. For a nested list lst that contains sublists of the form [n1, n2, n3] for integer values n1, n2
and n3,
a. Give a Python instruction that determines the length of the list
b. Give Python code that determines how many total integer values there are in list lst
c. Give Python code that totals all the integer values in list lst
d. Give an assignment statement that assigns the second integer of the third element (sublist)
of nested list to the value 12.
2. Write a Python program that prompts the user to enter types of fruit, and how many
pounds of fruit there are for each type. The program should then display the information
in the form fruit, weight listed in alphabetical order, one fruit type per line as shown
below,
[‘apple’,’ 6 lbs’ ]

[‘banana’, ‘11 lbs’]

etc.

3. Create two 2D matrix and find the sum.

You might also like