A list in Python may be a collection of things, which can be of any type 🔢 (strings 🔤, integers , floating point numbers, etc.). Python Lists are ordered , mutable , and permit duplicate elements 🔁. They are made utilizing square brackets [], and commas separate things.
What is List in Python?
List in Python are a data structure that can hold different things and are simple to control and work with. With the assistance of lists in Python, designers can rapidly construct productive applications that can store, prepare, and oversee huge sums of data.
One industry example to explain a python list is in the retail industry, specifically in the inventory management system. The list may look something like this:
In this framework, the store director or stock controller keeps a list of all the items or things accessible within the store. This list regularly incorporates the product name, description, quantity, cost, and other significant points of interest.
For occasion, let's say that a clothing store includes a list of all the T-shirts they have in stock.
Men's plain white T-shirt - Small - $15
Men's plain white T-shirt - Medium - $15
Men's plain white T-shirt - Large - $15
Men's black graphic T-shirt - Small - $20
Men's black graphic T-shirt - Medium - $20
Men's black graphic T-shirt - Large - $20
Women's red graphic T-shirt - Small - $18
Women's red graphic T-shirt - Medium - $18
Women's red graphic T-shirt - Large - $18
In this case, the list is a reference for the store's stock. The store supervisor can rapidly check the list to see which items are in stock and their costs. Furthermore, the list can track stock levels and make educated choices, almost restocking certain things or altering costs.
Overall, lists are valuable tools in various industries and can be used to organize and manage information efficiently.
How to Create a List in Python?
Creating a list in Python is a straightforward process. The syntax for creating a list is simple and intuitive, making it a great way to store data in a structured format .
Python Syntax example:
Loading...
Run
Examples of list in python:
Loading...
Run
We can also create an empty list and add elements to it later:
Loading...
Run
In this example, we first create an empty list called "numbers" using empty square brackets. We can add elements to this list later.
List() function in Python
In Python, the list() function may be a built-in function that can be utilized to form a modern list from an iterable object such as a tuple, string, or set. The list() function takes the iterable object as an argument and returns a new list that contains all the components from the iterable object in the order they appear up 🔢.
Creating List from String:
Loading...
Run
How to Access List Items?
In Python, you can access individual elements in a list by utilizing their index values. The index value of the primary element in a list is 0, and the second element has a list of 1, and so on. To induce a specific element in a list, you'll utilize the square bracket notation 🔢 with the index value inside the brackets.
Loading...
Run
In this example, we have a fruit list containing four elements. We access each element by using its index value inside the square brackets. The first element in the list has an index value of 0, so fruits[0] returns "apple". Similarly, fruits[1] returns "banana", fruits[2] returns "orange", and fruits[3] returns "grape".
You can also use negative index values to access elements from the end of the list. The last element in a list has an index value of -1, and the second-to-last element has an index value of -2, and so on.
Loading...
Run
In this case, we get to each element within the list by utilizing its negative index value inside the square brackets. The last element within the list has an index value of -1, so fruits[-1] return "grape". Additionally, fruits[-2] returns "orange", fruits[-3] returns "banana", and fruits[-4] returns "apple”.
List Slicing in Python
List slicing is a way to extract a portion of a list in Python. You can slice a list by specifying a range of index values inside the square brackets using the start:stop: step, the same as string slicing notation.
Here's how it works:
Loading...
Run
In this example, we have a fruit list containing five elements. We use the slice notation to extract a portion of the list and assign it to a new variable.
How Python list manipulation is done?
In Python, we have the following methods for list manipulation:
append(): Adds an element to the end of a list.
Loading...
Run
insert(): Inserts an element at a specific position in a list.
Loading...
Run
extend(): Adds multiple elements to the end of a list.
Loading...
Run
pop(): Removes an element from a specific position in a list and returns it.
Loading...
Run
remove(): Removes the first occurrence of a specific element from a list.
Loading...
Run
index(): Returns the index of the first occurrence of a specific element in a list.
Loading...
Run
sort(): Sorts the elements in a list in ascending order.
Loading...
Run
These methods allow adding, removing, or modifying elements in a list as needed.
Popular Build-In functions for list
Here are some of the popular built-in functions for lists in Python:
Len(): Returns the number of elements in a list.
Loading...
Run
min(): Returns the smallest element in a list.
Loading...
Run
max(): Returns the largest element in a list.
Loading...
Run
sum(): Returns the sum of all elements in a list.
Loading...
Run
sorted(): Returns a sorted list of elements.
Loading...
Run
reversed(): Returns a reversed list of elements.
Loading...
Run
enumerate(): Returns a list of tuples containing the index and element of each element in a list.
Loading...
Run
These built-in functions can be very useful when working with lists in Python. They allow you to perform common operations such as finding the length of a list, sorting the elements, or iterating over the list with an index.
Conclusion:
This lesson gives an outline of list in Python, including how to create and manipulate them, access list items, and utilize list cutting. It too covers prevalent built-in functions for lists, len(), min(), max(), and sum().
Key takeaways:
A list may be a collection of items in Python, which is of any type (strings, integers, floating point numbers, etc.).
Lists are ordered, mutable, and permit duplicate elements.
They are made utilizing square brackets [] and commas separate items.
An empty list can be created, and elements can be added afterward.
To get to a particular element in a list, we can utilize the square bracket notation with the index value inside the brackets.
We can also utilize list slicing to extract a portion of a list by indicating an extension of list values.
The list() function is a built-in function to create a new list from an iterable object, such as a tuple, string, or set.
Quiz:
What is a List in Python?
A collection of ordered and immutable elements
A collection of ordered and mutable elements
A collection of unordered and mutable elements
A collection of unordered and immutable elements
Answer: b. A collection of ordered and mutable elements.
What is the syntax to create a list in Python?
variable_name = (list of elements separated by comma)
variable_name = [list of elements separated by comma]
variable_name = {list of elements separated by comma}
variable_name = "list of elements separated by comma"
Answer: b. variable_name = [list of elements separated by comma]
How can we create an empty list?
list_name = []
list_name = list()
Both a and b
Neither a nor b
Answer: c. Both a and b
What is the index value of the last element in a list?
-1
0
1
2
Answer: a. -1
Which of the following is not true about lists in Python?
Lists are mutable
Lists can contain elements of different types
Lists are ordered
Lists do not allow duplicate elements
Answer: c. Lists are ordered
Which of the following is a valid way to access the second-to-last element in a list in Python?
list_name[-1]
list_name[-2]
list_name[1]
list_name[2]
Answer:b. list_name[-2]
Module 4: Data Structures in PythonLesson 4: List in Python
Module 4: Data Structures in PythonLesson 4: List in Python
Top Tutorials
Data Science
SQL
The SQL for Beginners Tutorial is a concise and easy-to-follow guide designed for individuals new to Structured Query Language (SQL). It covers the fundamentals of SQL, a powerful programming language used for managing relational databases. The tutorial introduces key concepts such as creating, retrieving, updating, and deleting data in a database using SQL queries.
Learn Data Science for free with our data science tutorial. Explore essential skills, tools, and techniques to master Data Science and kickstart your career
Master the basics of statistics with our applied statistics tutorial. Learn applied statistics techniques and concepts to enhance your data analysis skills.