A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item.
- 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.
- Insertion order preserved.
- Duplicate objects are allowed.
- Heterogeneous objects are allowed.
- List is dynamic because based on our requirement we can increase the size and decrease the size.
- 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. positive index means from left to right where as negative index means right to left.
[10,"A","B",20, 30, 10]
- List objects are mutable. i.e., we can change the content.
Creation of List Objects
1. We can create empty list object as follows.
list= print(list) print(type(list))
 <class 'list'>
2. If we know elements already then we can create list as follows list = [10, 20, 30, 40].
3. With Dynamic Input
list=eval(input("Enter List:")) print(list) print(type(list))
py test.py Enter List:[10,20,30,40] [10, 20, 30, 40] <class 'list'>
4. With list() Function
l=list(range(0,10,2)) print(l) print(type(l))
py test.py [0, 2, 4, 6, 8] <class 'list'>
s="ashok" l=list(s) print(l)
py test.py ['a', 's', 'h', 'o', 'k']
5. With split() Function
s="Learning Python is very very easy !!!" l=s.split() print(l) print(type(l))
py test.py ['Learning', 'Python', 'is', 'very', 'very', 'easy', '!!!'] <class 'list'>
Sometimes we can take list inside another list, such type of lists are called nested lists.
[10, 20, [30, 40]]
Lists are great to use when you want to work with many related values. They enable you to keep data together that belongs together, condense your code, and perform the same methods and operations on multiple values at once.
When thinking about Python lists and other data structures that are types of collections, it is useful to consider all the different collections you have on your computer: your song playlists, your browser bookmarks, your emails, the collection of videos you can access on a streaming service, and more.