Python List β An Introduction
What is a Python List?
A list is normally any collection of values. It is a data structure composed of items arranged in a linear fashion, each of which can be accessed from an index. Adding and removing items in a list varies from one programming language to another. Python list is the most popular way to store multiple values ββin one go. For example student list, consumer list, artist list, transaction list, etc.
To learn more about Python, read our blog β What is Python?
Each list has the same basics:
Items: Items are the real values ββwithin each position of the list.
Length: Length is the size of the list (how many items the list has).
Index: Index is the position of the element.
Best-suited Python courses for you
Learn Python with these high-rated online courses
Characteristics of Python List
A Python list is:
- Ordered
- Mutable
- Dynamic
It:
- May contain any arbitrary objects
- List elements can be accessed by index
- Can be nested to arbitrary depth
Check out the most commonly asked Python Interview Questions and Answers
List Methods
Len ()
This method returns the length of an object.
Index()
Returns the position of the element that we have entered for your search, if there were two equal elements, it would only return the position of the first.
Append ()
This method adds an element to the end of the list.
Count ()
With this method, we can know the number of times an element is repeated in our list.
Extend ()
With Extend () method we can add elements from one list to another, but unlike append (), as new elements within the new list.
Remove ()
Remove () method allows to delete an element from the list indicating it as an argument. Remove () will only remove the first occurrence of the element.
Insert ()
With this method, we can add an element to our list but unlike append (), we can insert it in the position we want within the index.
Popular Python Course Providers:
Top Python Courses by Udemy | Popular Python Courses by Coursera |
Top Python Courses by Udacity | Popular Python Courses by PluralSight |
Creating Python Lists
In Python, a list is represented as a sequence of objects separated by a comma and enclosed in square brackets [], so an empty list, for example, can be represented by square brackets with no content.
Letβs create a newly populated list, for example:
new_list = [1,2,3,4]
new_list
[1, 2, 3, 4]
Or we can just use the method to add anything you want to the list: append()
new_list.append(5)
new_list
[1, 2, 3, 4, 5]
Also Read β What is Data Science?
If you need to add multiple items to the same list, the method will come in handy. You just need to pass the list of items to add to the method, as shown below: extend() extend
new_list.extend([6,7,8])
new_list
[1, 2, 3, 4, 5, 6, 7, 8]
As you can see, creating a list and attaching it to other items is very easy. You can accomplish this task without having to make multiple calls to the method. .append()
Check out the Top Reasons and Resources to Learn Python
Similarly, you can use a for loop or list comprehension to add multiple items to a list. For example, we will have to write the following code snippet to create a list of squares for the integers 1-20.
new_list = []
for i in range(1,20):
new_list.append(i)
Or more simply
new_list = [i for i in range(1,20)]
Also Read: Python Projects for Beginners
Using Lists in Python
A list is capable of storing multiple values ββin the same variable; we have the perfect element to store the information of all the users. We can create a list of any size capable of containing the names of the users and another list with the identifications of the people.
Note: Alternatively, we could create a two-column table (or matrix) that contains the names in the first column and the identifications in the second.
If you have recently completed a professional course/certification, click here to submit a review.
Rashmi is a postgraduate in Biotechnology with a flair for research-oriented work and has an experience of over 13 years in content creation and social media handling. She has a diversified writing portfolio and aim... Read Full Bio