What is list manipulation in Python
Introduction of list
- In Python, a list is a kind of container that contains collection of any kind of values.
- A List is a mutable data type which means any value from the list can be changed. For changed values , Python does not create a new list.
- List is a sequence like a string and a tuple except that list is mutable whereas string and tuple are immutable.
- In this chapter we will see the manipulation on lists. We will see creation of list and various operation on lists via built in functions.
List Creation
- List is a standard data type of Python. It is a sequence which can store values of any kind.
- List is represented by square brackets “ [ ] “ For ex -
- [ ] Empty list
- [1, 2, 3] integers list
- [1, 2.5, 5.6, 9] numbers list (integer and float)
- [ ‘a’, ‘b’, ‘c’] characters list
- [‘a’, 1, ‘b’, 3.5, ‘zero’] mixed values list
- [‘one’, ’two’, ’three’] string list In Python, only list and dictionary are mutable data types, rest of all the data types are immutable data types.
Creating a List
To create a list , put a number of expressions in square brackets. This is , use square brackets brackets to indicate the starts and end of the list, and separate the items by the commas.
[]
[2,4,6]
['ajay','patel']
[1,2.5,3,4.0]
To create a list you can write in the form given below.
L=[]
L=[value,.....]
Create Empty List in Python
To create an empty list in Python, use one of these.
- Create an empty list using square brackets [ ].
- Create an empty list using list() constructor.
Create an empty list using square brackets
To create an empty list in Python, define the empty pair of square brackets.
data = []
We can verify that the data list is empty by checking its length using the len() function. If it returns 0, that means the list is empty; otherwise, not.
data = []
print(len(data))
Output
0
Create an empty list using list() constructor
Alternatively, to create an empty list, use the type constructor list(), which creates a new list object. In the list constructor, If no argument is given, the constructor creates a new empty list, [ ].data = list()
This empty list will have a length of 0.
data = list()
print(len(data))
Output
0
Example:
# Python program to demonstrate
# Creation of List
# Creating a List
List = []
print("Blank List: ")
print(List)
# Creating a List of numbers
List = [10, 20, 14]
print("\nList of numbers: ")
print(List)
# Creating a List of strings and accessing
# using index
List = ["Ajay", "Kumar", "Patel"]
print("\nList Items: ")
print(List[0])
print(List[2])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [['Ajay', 'kumar'], ['Patel']]
print("\nMulti-Dimensional List: ")
print(List)
Output:
Blank List:
[]
List of numbers:
[10, 20, 14]
List Items
Ajay
Patel
Multi-Dimensional List:
[['Ajay', 'Kumar'], ['Patel']]
Nested List:
A nested list is a list within a list. Python provides features to handle nested list gracefully and apply common functions to manipulate the nested lists. In this article we will see how to use list comprehension to create and use nested lists in python.
# creating list
nestedList = [1, 2, ['a', 1], 3]
# indexing list: the sublist has now been accessed
subList = nestedList[2]
# access the first element inside the inner list:
element = nestedList[2][0]
print("List inside the nested list: ", subList)
print("First element of the sublist: ", element)
No comments:
Post a Comment
for more information please share like comment and subscribe