IKH

Introduction

  • If we want to represent a group of individual objects as a single entity where insertion order preserved and duplicates are allowed, then we should go for List.

Properties

  • Insertion order preserved.
  • Duplicate objects are allowed.
  • Heterogeneous objects are allowed.
  • List is dynamic, it means based on our requirement we can increase or decrease the size.

Remark

  • In list the elements will be placed within square brackets and with comma separator.
  • We can differentiate duplicate elements by using index and we can preserve insertion order by using index. Hence index will play very important role.
  • Python supports both positive and negative indexes. +ve index means from left to right where as negative index means right to left.
  • List object are mutable i.e. we can change the content.

Creation of List Object

  • By following methods we can create list object.

Create empty list

Python
list=[] 
print(list) 
print(type(list))

Output

PowerShell
[]
<class 'list'>

With static input

  • If we know elements already then we can create list as follows
Python
list=[10,20,30,40]
print(list)
print(type(list))

Output

PowerShell
[10, 20, 30, 40]
<class 'list'>

With dynamic input

Python
list=eval(input("Enter List:")) 
print(list)
print(type(list))

Output

PowerShell
Enter List:[10,20,30,40]
[10, 20, 30, 40]
<class 'list'>

With list() function

Python
l=list(range(0,10,2)) 
print(l) 
print(type(l))

Output

PowerShell
[0, 2, 4, 6, 8]
<class 'list'>

Example

Python
s="ajay" 
l=list(s) 
print(l)

Output

PowerShell
['a', 'j', 'a', 'y']

With split() function

Python
s="Learning Python is very very easy !!!"
l=s.split()
print(l)
print(type(l))

Output

PowerShell
['Learning', 'Python', 'is', 'very', 'very', 'easy', '!!!']
<class 'list'>

Remark

  • Sometimes we can take list inside another list, such type of lists are called nested lists. example [10,20,[30,40]]

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error