List is one of the data types in python. List is almost like array but, the input methods are totally different. So, here we are gonna see “the input methods of list in python.”
There are several ways to input our data into list in python. But, we are taken below methods only.
- Using for loop and append function
- Using exception handling
Input methods of list in python:
1. Input using for loop and append function
You all know about array input in c, that for loop is used here to input the value and the append function is used to store the data into respective list. you should know “how many elements are you want ti store in list?” to use this method. Find the example here https://github.com/gurusabarish/python-programs/blob/master/list_input_1.py Explanation,
- Declare the variable as list.
- Get the count of the elements as integer.
- Get each elements using for loop and range function.
- Store the element using append in list.
- Print the list.
![Input methods of list in python | List input in python](https://programerdid.wordpress.com/wp-content/uploads/2020/07/94de6-capture.png)
![Input methods of list in python | List input in python](https://programerdid.wordpress.com/wp-content/uploads/2020/07/f06aa-capture1.png)
2. Input using exception handling
In this method you don’t need to know the count of the elements. But, you should only get the integers using this method. Find the example here https://github.com/gurusabarish/python-programs/blob/master/list_input_2.py Explanation,
- Declare the variable as list into try section.
- Get the elements using while until you press the enter button.
- In exception section, print the list.
![Input methods of list in python | List input in python](https://programerdid.wordpress.com/wp-content/uploads/2020/07/65729-capture.png)
![Input methods of list in python | List input in python](https://programerdid.wordpress.com/wp-content/uploads/2020/07/782c4-capture1.png)