List in Python

List is use to store group of items or elements. List is similar to an array but there is a difference between them. Array is use to store elements of same data type whereas list is use to store elements of different data types.

For example, array can be used to store numbers. All elements in the below array has integer data type.

numbers = array('i' , [1,3,4,5,6,7])

But we cannot use array to store employee information. Employee information include name, age, salary, address. Employee information has elements of mixed data types such as name and address is of string data type whereas age is an integer data type. For this, list is the option to store elements having integer and string data type.

List can store elements of different data types. Below is the list of employee information.

emp=[‘Rohan’,30,’India’,345555]

emp is the name of a list. This emp list contains elements such as name, age, address and salary. ‘Rohan’ is the name of employee, 30 is the age, ‘India’ is the address and 345555 is the salary.

Creating a  list. 

List is simple to create. Below is the syntax for creating list.

list_name = [‘item1′,item2,’item3,’item4’]

Example city list.

city = ['Mumbai','MH','400001']

city is the name of the list. This list contains three elements city name, state and pincode. Each item or element in a list is separated by comma. All items or elements must be included in square brackets. String elements are enclosed inside quotes.

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s