In this tutorial, learn to how to create list variable in Python. Access, iterate and find the length of the list elements. Also, find the length of the list variable using the Python built-in functions.
Learn with the list examples with the results given in the output section. Create your own list variable with the elements and operate with the different methods given here. So, let’s start learning each section one by one below.
What is List Variable in Python
A list variable is a collection of elements of different data types. The elements are enclosed within the square brackets([]). Lists are declared just like the array in other programming languages like Java, PHP, etc.
The list may contain the elements of different data types like string and integers. The list elements are ordered and changeable or mutable with different operations of python.
Each element of the list is arranged with a sequence whose assigned index starts from zero(0). The elements are comma-separated and can be of different data types.
Creating A List in Python
To create a list in Python, you have to place elements of different data types. You can place integer, string, and objects also like the list elements. There is no limit to place elements in the list. You can place as many numbers of elements as you want.
In order to create a list in Python, you have to place items and enclosed them within the square brackets([]). See the example below showing the created list using Python.
1 2 |
myList = ["one", 11, "Two", 23.9, "Three"]; print(myList); |
Output
The above example showing the list item with the 5 elements. There are two integers and three string items in the list variable. These items make a complete ordered list with elements of the different data type.
Accessing List Element
The list items are also indexed like array elements of other programming languages. If you want to access the elements of the list variable in Python. You have to use the index operator([]) and the slice operator([:]) of Python.
Access List Elements Using Index Operator
If you want to get the list elements in the output. You have to use the below-given example contains 5 elements. Suppose, you have to get first and the second list element. Place the indexed value 0 and 1 as given in the example.
1 2 3 |
myList = ["one", 11, "Two", 23.9, "Three"]; print(myList[0]); print(myList[1]); |
Output
11
The above example prints the first and the second element of the list in Python. The first element is numeric and the second element is the string.
If you want to get the elements within index range. You have to use the slice operator whose example is given below.
Access List Elements Using Slice Operator
The slice operator requires the range to print all elements within the range. If you want to print the first three elements of the list, you have to use the below-given example.
1 2 |
myList = ["one", 11, "Two", 23.9, "Three"]; print(myList[0:3]); |
Output
The above example showing the three elements enclosed within the square([]) bracket. The first and the third element is string while the second element is an integer.
Iterate Through List Items in Python
If you want to perform iteration through each element of the array in Python. You have to use the below-given example which uses for loop. However, you can also use the Python while loop to iterate over each element of a list.
There are five elements in the list variable. If you use for loop, the iteration performs through each element for 5 times and print each element.
1 2 3 |
myList = ["one", 11, "Two", 23.9, "Three"]; for l in myList: print(l); |
Output
11
Two
23.9
Three
The above example prints each element in the output with the single in the single line. You may also like to read various method to loop through each element. You have to read the tutorial on how to loop over each element of a list in Python.
Find Length of List
To find the length of the list in Python, you have to use the len()
of Python. The function takes one argument to pass which is the list. If you want to print the length in the output, you have to use the Python print statement.
1 2 |
myList = ["one", 11, "Two", 23.9, "Three"]; print(len(myList)); |
Output
The above example prints the length of the list in the output. The length of the list is 5 as it contains 5 items including string and integers.
Add List Element
If you want to add or insert elements to the list in Python. You can use Python append()
and insert()
. You can add only one element using these two functions of Python.
However, if you want to add more than an item in one go. Use the concatenation plus(+) operator and extend function. The details of these operators and functions are given in the section below.
Insert Element to the End of the List Using append()
The append function takes the single argument as an item to insert at the end of the list. Use the below example to add the list element to the end.
1 2 3 |
myList = ["one", 11, "Two", 23.9, "Three"]; myList.append(14); print(myList); |
Output
The above example adds 14 as the last element of the Python list. However, the append function adds the last element everytime you use it in Python.
Also, use the print statement to print the list with the appended element in the output. In addition to this, you can also add the item to your desired location using Python. You have to use the Python insert() whose details are given below with examples
Add Element to the Desired Location of the List Using insert()
The insert()
function takes two arguments to add the element to the list. The first argument is the ‘index’ where which you want to add the element. The second argument is the element which you want to insert to the list in Python.
1 2 3 |
myList = ["one", 11, "Two", 23.9, "Three"]; myList.insert(2,9); print(myList); |
Output
The above example adds element 9 to the list in the 2nd index. List in the output given above contains the inserted element to the specified location.
Update List Element
Updating a list element requires you to assign the new value to a particular index. You have to follow the example assigning a value to index 1 with a new element to change.
You can change other items of the list using the below method. It’s showing the method to change a single element of the list in Python. However, you can update more elements together by assigning with the same method to different index element.
1 2 3 |
myList = ["one", 11, "Two", 23.9, "Three"]; myList[1] = 13; print(myList); |
Output
The above example showing the output with the updated element. The new element 13 replaced the old element 11 after code execution.
Delete/Remove List Element
There are various methods available in Python to remove elements from the list. If you want to know and learn about these elements, read below examples with descriptions.
Delete List Element Using del()
You can delete the element by using the del()
and passing variable with the required index. Use the below-given example to delete the element. You can change the index as per your requirement.
1 2 3 |
myList = ["one", 11, "Two", 23.9, "Three"]; del myList[1]; print(myList); |
Output
The above example requires the index to pass as an argument to remove the item. However, if you want to remove the item by passing the item as the argument. You can use Python remove() whose details given below.
Mention the element to Delete Using remove()
If you want to mention the element which you want to remove from the list. You have to use the Python remove()
and pass the element as the argument of the function. See the example below to get the idea of using the remove() function.
1 2 3 |
myList = ["one", 11, "Two", 23.9, "Three"]; myList.remove("one"); print(myList); |
Output
The above example prints the list in the output after the deletion of the mentioned element. It removes the first element from the list of Python.
Delete List Element Using pop()
If you want to remove or delete the last element from the list. You have to use the below-given example using the pop()
.
1 2 3 |
myList = ["one", 11, "Two", 23.9, "Three"]; myList.pop(); print(myList); |
Output
The above example showing the list after the deletion of the last element. You may also like to how to create a string variable in Python.
Hope you like this tutorial of how to create list variable in Python. If you have any query regarding the tutorial, please comment below.
Also tell me, which method you are using to create your own list on Python.
how would i make my list scan for repeated properties for example:
mylist = []
x = input()
mylist.append(x)
and then once a element has been repeated it does something else?