How to Create a List in Python – (Index, Append, Reverse, Slicing )

Python Code

Lists are the ordered sequence that can hold a variety of object types. In this code tutorial, you will learn How to create a List in Python?

How to create a list in Python from values input by the user?

Method 1


#Define empty list
list =[]
#Take input from the user
value1 = input("Enter the value")
value2 = input("Enter the value")
value3 = input("Enter the value")
value4 = input("Enter the value")
#append the list
list.append(value1)
list.append(value2)
list.append(value3)
list.append(value4)
#output the list
print(list)

Method 2

Using  For loop Iteration


#define the range
lenght_range = int(input("Enter the length of list:-"))
#iterate the loop in range
for i in range(0,lenght_range) :
     list_value = input("Enter the value:")
#insert values to the list
     list.append(list_value)

#display the list
print(list)

 

OutPut

Method 1


Enter the value:10
Enter the value:150.0
Enter the value:Big Data
Enter the value:NLP
['10', '150.0', 'Big Data', 'NLP']

Method 2

Enter the length of list:-5
Enter the value:4
Enter the value:Data Science
Enter the value:5.0
Enter the value:AI
Enter the value:Machine Learning
['4', 'Data Science', '5.0', 'AI', 'Machine Learning']

 

Full Explanation

Method 1

First of all, you have to create an empty list ( list = [ ] ). In fact, It doesn’t contain any values inside. Then we will ask the user for giving the values through input.The syntax for taking input is variable_name = input(“Enter the value :”). After that, all the input values will be added to the list one by one ( list.append(variable_name)).

Method 2

The Method 1 is good for the small number of values. But for the large values, you will use iteration for the creation of the list. In addition, It will also use the range() method for the iteration. First of all, define the maximum length of the range length_range = int(input(“Enter the length of list:-“)).  The statement for i in range(0,lenght_range) will Iterate the loop from o to length_range -1 . Inside the iteration, we will ask the user to input the values for the list and list.append(list_value) will add all the values to the empty list ( list [ ]).

 

Extra  Examples

1. Find out the specific elements from the list


#Find out the specific elements from the list 
list = ['Data Science Learner','Big data', '401.2', '40'] # list
print(list[2]) # display list value at index 2

Output

401.2

Explanation

When you type the statement list[ 2 ], then python interpreter will search the list for the element at location(index) 2. Since index starts from 0 to length of list -1.  

list[0] : ‘Data Science Learner’list[1] : ‘Big data’list[2] : ‘401.2 ‘  and list[3] : 40

Therefore print(list[2]) will give 401.2 as the output.

2. How to reverse a list in python?

You can reverse the list using the list.reverse() method and slicing the list.

Method 1: Using list.reverse()


#Reverse the list 
list = ['Data Science Learner','Big data', '401.2', '40'] # list
list.reverse() # reverse the elments
print(list) # print the list

Output

['40', '401.2', 'Big data', 'Data Science Learner']

Method 1: Through Slicing


#Reverse the list  
list = ['Data Science Learner','Big data', '401.2', '40'] # list
print(list[::-1]) # reverse display of the list

Output

['40', '401.2', 'Big data', 'Data Science Learner']

Explanation

Negative numbers inside list[] start from the end. The statement list[:: -1] will reverse the list, it means to start from end till the first element.

list[low: high: step], it is a slicing combination. Low describes how many elements of the list.  High defines how many elements from the beginning till the High-1. The step is the difference between high and the low. The default value of step is 1. The following combination is generally in use.

Postive Combination

list[: : : ] – Create copy of whole list.

list[low:high ] – Start after the low value  till high-1.


list = ['Data Science Learner','Big data', '401.2', '40'] # list
print(list[1:3])

Output

['Big data', '401.2']

list [: high]  – Values start from the beginning through high -1.


list = ['Data Science Learner','Big data', '401.2', '40'] # list
print(list[:2])

Output

['Data Science Learner', 'Big data']

list[low:high:step] : Start after low to end-1 by  step.


list = ['Data Science Learner','Big data', '401.2', '40'] # list
print(list[1:4:2])

Output

['Big data', '40']

Negative Combination

list[-1]- Only the last element in the lists.  list[-2:] – Last two elements. list[:3]–  Contain all the elements except the last three elements. list[:: -1] – All the items in a reverse manner. list[1:: -1 ] – Display the first two elements in a reverse way. list[: -2:-1] – The last two elements in a reverse way.


list = ['Data Science Learner','Big data', '401.2', '40'] # list
print(list[-1])
print(list[-2])
print(list[:3])
print(list[:: -1])
print(list[1:: -1 ])
print(list[: -2:-1])

Output

40
['401.2', '40']
['Data Science Learner', 'Big data', '401.2']
['40', '401.2', 'Big data', 'Data Science Learner']
['Big data', 'Data Science Learner']
['40']

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner