Python Programming - List Manipulation

You can learn about List Manipulation in Python Programs with Outputs helped you to understand the language better.

Python Programming – List Manipulation

List

The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number – its position or index. The first index is zero, the second index is one, and so forth.
A list is a sequence of multiple values in an ordered sequence. It is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. Each element or value that is inside of a list is called an item. Like a string, a list is a sequence of values. In the string, the values are characters; in a list, they can be of any type. Lists are more general than strings.

a. Strings are always sequences of characters between quotes, whereas lists can contain values of any type between square brackets [ ]. Lists can contain values of mixed data types. A single list can contain numbers, strings, or other lists.

b. Unlike strings, which are immutable, it is possible to change individual elements of a list. Lists are mutable, which means that items in a list can be modified by assigning new values.

c. Like string indices, list indices start at 0, and lists can be sliced, concatenated, and so on.

d. List is a sequence that can be indexed into, and that can grow and shrink by adding elements or deleting elements from the list.

  • A list is an ordered collection of values.

Objects and Values

If you execute these assignment statements:
a = ‘apple’
b = ‘apple’
You know that a and b both refer to a string, but you do not know if they refer to the same string. There are two possible states:
(a) In the first case, a and b refer to two different objects that have the same value.
(b) In the second case, they refer to the same object. To check whether two variables refer to the same object or not, you can use the is the operator.
>>> a = ‘apple’
>>> b = ‘apple’
>>> a is b
True
In this example, Python only created one string object, and both a and b refer to it. But when you create two lists, you get two objects:
>>> a = [1, 3, 5]
>>> b = [1, 3, 5]
>>> a is b
False
In this case, you can say that two lists are equivalent because they have the same elements, but not iden-tical, as they do not refer to the same object. If two objects are identical, they are also equivalent, but if they are equivalent, they are not necessarily identi-cal.

Aliasing

A circumstance where two or more variables refer to the same object is called aliasing. If a refers to an object and you assign a to b, then both variables refer to the same object:
>>> a = [1, 3, 5]
>>> b = a >>> b is a
True
The association of a variable with an object is called a reference. In this example, there are two references to the same object.
An object with more than one reference has more than one name, so you say that the object is aliased. If the aliased object is mutable, changes made with one alias affect the other:
>>> b[1] = 7
>>> print(a)
[1, 7, 5]

Counting Frequency of elements in a list

Example
Program to Find the Frequency of Numbers in a List.

# To find the frequency of numbers in a list
my_list = [ ] #empty list
print(“Enter list of five integers”)
for i in range(5):
print(“Enter integer”, i)
userinput=int(input())
my_list.append(userinput)
print(“The list is: “,my_list)
length=len(my_list)
element=int(input(“Enter element :”))
c=0
for i in range(0,length-1):
if element==my_list[i]:
c+=1
if c == 0:
print(element, “not found in a given list”)
else:
print(element, “has frequency as”, c, “in given list”)
RUN
>>>
Enter list of five integers
Enter integer 0
2
Enter integer 1
3
Enter integer 2
2
Enter integer 3
2
Enter integer 4
5
The list is: [2, 3, 2, 2, 5]
Enter element:2
2 has frequency as 3 in the given list
>>>

Let’s Try

Write the output of following program given in Figure 7.18.

#To find the frequency of elements in a list
my_list=eval(input(“Enter list: “))
#print(‘Original List : ‘,my_list)
length=len(my_list)
element=int(input(“Enter element :”))
c = 0
for i in range(0,length-1):
if element==my_list[i]:
c+ = 1
if c == 0:
print(element, “not found in a given list”)
else:
print(element, “has frequency as”, c, “in given list”)

Leave a Reply

Your email address will not be published. Required fields are marked *