Table of Contents
what is list in python with example :
Welcome everyone, today we are going to learn what is list in python?Before going to learn if you want to learn python for free then click on python tutorials point.The most Important thing about a lists is that items of in list not need always be the same data type.
In this post, we will discuss python is in list.
Definition of Python list :
A list is a data type in which you to store various types data. remember List is a compound data types in python.
In a list we can be use integer, float items i.e.different-two data types under a lists.
In Python the most basic data structures is the sequence. Each elements of a sequence is assigned its positions or index. The first index in python is zero, the second index is increment by one, and so on.
Python also has six built-in type of sequences like as lists and tuples.
There are different operation include indexing,multiplying, slicing, and checking ,adding.
Welcome everyone, today we are going to learn what is list in python?Before going to learn if you want to learn python for free then click on python tutorials point.The most Important thing about a lists is that items of in list not need always be the same data type.
In this post, we will discuss python is in list.
Definition of Python list :
A list is a data type in which you to store various types data. remember List is a compound data types in python.
In a list we can be use integer, float items i.e.different-two data types under a lists.
In Python the most basic data structures is the sequence. Each elements of a sequence is assigned its positions or index. The first index in python is zero, the second index is increment by one, and so on.
Python also has six built-in type of sequences like as lists and tuples.
There are different operation include indexing,multiplying, slicing, and checking ,adding.
Python is in Lists :
How to create list in python ?
Creating a lists is very simple by putting different comma-separated value between square bracket.
For example −
list1 = ['python', 'matlab', 2015, 2020];
list2 = [5, 6, 7, 8, 9 ];
list3 = ["p", "q", "r", "s"]
How to create list in python ?
Creating a lists is very simple by putting different comma-separated value between square bracket.
For example −
list1 = ['python', 'matlab', 2015, 2020];
list2 = [5, 6, 7, 8, 9 ];
list3 = ["p", "q", "r", "s"]
Some Basic List Operation:
Python Expression
Results
Description
len([1, 2, 3,4])
4
Length
[1, 2, 3] + [4, 5, 6] +[7,8,9]
[1, 2, 3, 4, 5, 6,7,8,9]
Concatenation
['pythonslearning!'] * 2
['pythonslearning!', 'pythonslearning']
Repetition
3 in [1, 2, 3]
True
Membership
for x in [pythonslearning, 2, 3]: print x,
pythonslearning 2 3
Iteration
Python Expression | Results | Description |
---|---|---|
len([1, 2, 3,4]) | 4 | Length |
[1, 2, 3] + [4, 5, 6] +[7,8,9] | [1, 2, 3, 4, 5, 6,7,8,9] | Concatenation |
['pythonslearning!'] * 2 | ['pythonslearning!', 'pythonslearning'] | Repetition |
3 in [1, 2, 3] | True | Membership |
for x in [pythonslearning, 2, 3]: print x, | pythonslearning 2 3 | Iteration |
1. Create a List in Python
In the previous section we see How to create list in Python. Creating a lists is very simple by putting different comma-separated value between square bracket.
# list of floats
num_list = [11.22, 9.9, 68.34, 15.0]
# list of int, float and strings
mix_list = [1.13, 7, 5, "pythonslearning", 100, "love"]
# an empty list
nodata_list = []
List can have data items of different types or same types . Hence lists comes under compound data types.
In the previous section we see How to create list in Python. Creating a lists is very simple by putting different comma-separated value between square bracket.
# list of floats
num_list = [11.22, 9.9, 68.34, 15.0]
# list of int, float and strings
mix_list = [1.13, 7, 5, "pythonslearning", 100, "love"]
# an empty list
nodata_list = []
List can have data items of different types or same types . Hence lists comes under compound data types.
2. Accessing the items from a list
Syntax to access the list items:
list_name[index]
Example:
# a list of numbers
numbers = [24, 26, 28, 35, 40, 50]
# prints 24
print(numbers[0])
# prints 35
print(numbers[3])
# prints 50
print(numbers[5])
Output:
24
35
50
Points to Note:
1. The index cannot be a float number.
For example:
# a list of numbers
numbers = [10, 25, 30, 35, 40, 50]
# error
print(numbers[3.0])
Output:
TypeErrors: list indices always be integers not float
# a list of numbers
numbers = [11, 22, 33, 100, 200, 300]
# error
print(numbers[6])
Output:
IndexError: list index out of range
Syntax to access the list items:
Example:
# a list of numbers
numbers = [24, 26, 28, 35, 40, 50]
# prints 24
print(numbers[0])
# prints 35
print(numbers[3])
# prints 50
print(numbers[5])
Output:
24
35
50
Points to Note:
1. The index cannot be a float number.
For example:
# a list of numbers
numbers = [10, 25, 30, 35, 40, 50]
# error
print(numbers[3.0])
Output:
# a list of numbers
numbers = [11, 22, 33, 100, 200, 300]
# error
print(numbers[6])
Output:
python list index :
3. Negative Index to access the list items from the end :
Python allow you to use negative indexes in different ways:
Python allow you to use negative indexes in different ways:
3.1 Example of Negative indexes in Python
# a list of strings
my_list = ["python", "Matlab", "java", "Arduino"]
# prints "python"print(my_list[-1])
# prints "Matlab"print(my_list[-2])
# prints "Arduino"print(my_list[-3])
Output:
Matlab
Java
Arduino
# a list of strings
my_list = ["python", "Matlab", "java", "Arduino"]
# prints "python"print(my_list[-1])
# prints "Matlab"print(my_list[-2])
# prints "Arduino"print(my_list[-3])
Output:
Matlab
Java
Arduino
Matlab
Java
Arduino
4. How to get a sublist in Python using slicing
Using the slicing operation we can get a sub list from a lists in Python.
Using the slicing operation we can get a sub list from a lists in Python.
4.1 Slicing example
# numbers in list
number_list = [96, 97, 98, 99, 100]
# list items from 2nd to 3rd
print(number_list[1:3])
# lists item from beginning to third
print(number_list[:3])
# list items from fourth to end of lists
print(number_list[3:])
# Whole list
print(number_list[:])
Output:
[97,98]
[96,97,98]
[97,98,99,100]
[[96,97,98,99,100]
# numbers in list
number_list = [96, 97, 98, 99, 100]
# list items from 2nd to 3rd
print(number_list[1:3])
# lists item from beginning to third
print(number_list[:3])
# list items from fourth to end of lists
print(number_list[3:])
# Whole list
print(number_list[:])
Output:
[97,98]
[96,97,98]
[97,98,99,100]
[[96,97,98,99,100]
[97,98]
[96,97,98]
[97,98,99,100]
[[96,97,98,99,100]
5. List Operations
THERE ARE DIFFERENT TYPES OF OPERATION IN LIST OF PYTHON
We can see one by one
1. Addition
2. Update Element
3. Delete element
THERE ARE DIFFERENT TYPES OF OPERATION IN LIST OF PYTHON
We can see one by one
1. Addition
2. Update Element
3. Delete element
5.1 Addition
From the different method you can add element to a lists.
# numbers is in list
number_list = [96, 97, 98, 99]
# At the 4th location adding element 404
number_list.insert(3, 404)
# list: [96, 97, 98, 404, 99]
print(number_list)
# Adding element at the end of the listS 505
number_list.append(505)
# list: [96, 97, 98, 404, 99, 505]
Output:
[96, 97, 98, 404, 99]
[96, 97, 98, 404, 99, 505]
From the different method you can add element to a lists.
# numbers is in list
number_list = [96, 97, 98, 99]
# At the 4th location adding element 404
number_list.insert(3, 404)
# list: [96, 97, 98, 404, 99]
print(number_list)
# Adding element at the end of the listS 505
number_list.append(505)
# list: [96, 97, 98, 404, 99, 505]
Output:
[96, 97, 98, 404, 99]
[96, 97, 98, 404, 99, 505]
[96, 97, 98, 404, 99]
[96, 97, 98, 404, 99, 505]
5.2 Update elements
we can see on the bases of example:
# numbers in list
n_list = [96, 97, 98, 99]
# Changing the value of 2rd item
n_list[1] = 101
# list: [96, 101, 98, 99]print(n_list)
# Changing the values of 1st to 3rd items
n_list[0:3] = [12, 13, 14]
# list: [12, 13, 14, 99]
print(n_list)
Output:
[96, 101, 98, 99]
[12, 13, 14, 99]
we can see on the bases of example:
# numbers in list
n_list = [96, 97, 98, 99]
# Changing the value of 2rd item
n_list[1] = 101
# list: [96, 101, 98, 99]print(n_list)
# Changing the values of 1st to 3rd items
n_list[0:3] = [12, 13, 14]
# list: [12, 13, 14, 99]
print(n_list)
Output:
[96, 101, 98, 99]
[12, 13, 14, 99]
[96, 101, 98, 99]
[12, 13, 14, 99]
5.3 Delete elements
# list of numbers
number_list = [11, 23, 33, 46, 51, 68]
# Deleting 3nd element
del number_list[2]
# list: [11, 23, 46, 51, 68]print(number_list)
# Deleting elements from 2nd to 4th
del number_list[1:4]
# list: [11, 68]
print(number_list)
# Deleting the all list
del number_list
Output:
[11, 23, 46, 51, 68]
[11, 68]
Python List Method :
# list of numbers
number_list = [11, 23, 33, 46, 51, 68]
# Deleting 3nd element
del number_list[2]
# list: [11, 23, 46, 51, 68]print(number_list)
# Deleting elements from 2nd to 4th
del number_list[1:4]
# list: [11, 68]
print(number_list)
# Deleting the all list
del number_list
Output:
[11, 23, 46, 51, 68]
[11, 68]
Python List Method :
[11, 23, 46, 51, 68]
[11, 68]
Python List Method :
Method | Description |
---|---|
append() | This method Add an elements at the end of the lists |
clear() | This method Remove all the element from the lists |
copy() | This method Return a copy of the lists |
count() | This method Return the numbers of element with the specified value |
extend() | This method Add the element of a list to the end of the current lists |
index() | This method Return the index of the first elements with the specified value |
insert() | This method Add an elements at the specified positions |
pop() | This method Remove the elements at the specified positions |
remove() | This method Remove the first item with the specified value |
reverse() | This method Reverse the order of the lists |
sort() | This method Sort the lists |
Tags: What is a list in Python?,What are the list methods in Python?,How do you write a list in Python?,python is in lists,Accessing the items from a list,python list index
For similar kind of post click on python tutorials point and if you like this post then please comment and share.
BEST OF LUCK!!!
pagarsach14@gmail.com