How To Create A List In Python

Introduction

 
In this session, we are going to discuss how to create a list in Python. The following link will help you to understand the basics and environment setup of Python.
Assign List in the Variable name
 
Now, we are going to discuss how to assign a list of values to a variable.
 
CODE
  1. >>> myList = []  
  2. >>> myList  
  3. [ ]  
  4. >>> myList = ["PHP""Python""Java""Perl"]  
  5. >>> myList  
  6. ['PHP''Python''Java''Perl']  
  7. >>>   
List in Python
 
In the above screen, we have created the variable named “myList”. Initially, we have not assigned any value to the List. We declared the myList = [] and hit enter.
Then, you can see that an empty array has been created for myList variable on Shell.
 
Get specific value
 
If you want to get a specific value from the list, you can use the following Syntax on Python Shell.
 
Syntax
 
Variable_name[array_index]
 
Let’s discuss this with a live example.
 
CODE
  1. >>> myList[1]  
  2. 'Python'  
  3. >>> myList[2]  
  4. 'Java'  
  5. >>>   
List in Python
 
In the above screen, you can see that we have got a specific value from the list.
 
Add a new value to the List
 
If you want to add a new record into the List, you should use the append keyword. Find the below screenshot for reference.
 
List in Python
 
When you assign a new record, check it in the variable name on Shell.
 
CODE
  1. >>> myList  
  2. ['PHP''Python''Java''Perl']  
  3. >>> myList.append("New Language")  
  4. >>> myList  
  5. ['PHP''Python''Java''Perl''New Language']  
  6. >>>   
List in Python
 
Create a new variable
 
Here, we are going to create a new variable and extend the value from one variable to another.
 
Let’s create one more variable in Python Shell. The following steps are needed to be implemented in the next step.
  1. Create a new variable
  2. Assign new records to the List
  3. Select the variable name in Python Shell
CODE
  1. >>> age = ["100","101","102"]  
  2. >>> age  
  3. ['100''101''102']  
  4. >>>   
List in Python
 
Extend records
 
If you want to extend the record from one variable to another, you can use the “extend” keyword.
 
In the following example, we will extend the “age” records into “myList” variable.
 
CODE
  1. >>> myList.extend(age)  
  2. >>> myList  
  3. ['PHP''Python''Java''Perl''100''101''102']  
  4. >>>   
List in Python
 
As well as, you can use the existing variable name “age” records. It will not remove from the variable.
 
Remove a value from the List
 
If you want to remove the record from the List, you can use the “remove” keyword.
 
CODE
  1. >>> myList.remove("Java")  
  2. >>> myList  
  3. ['PHP''Python''Perl''100''101''102']  
  4. >>>   
List in Python
 
In the above screen, you can see that we have removed “Java” from the List.
 
Record Count in List
 
You can check the total counts in List by using the keyword len(variable_name).
 
CODE
  1. >>> len (myList)  
  2. 6  
  3. >>>   
List in Python
 
Thank you!


Similar Articles