How to create python list

How to create python list

How to create python list
In python a List can be created by placing all the elements inside a square bracket [ ], separated by commas.
A list can contain any number of elements and they may be of different types.

We can create a list by following types-

1. A list can be created from all integer values

list1 = [ 7, 5, 15, 20 ]

print( list1 )

Output

[ 7, 5, 15, 20 ]

2. A list can be created from all string values

list2 = [ ‘Indore’, ‘Bhopal’, ‘Jabalpur’ ]

print( list2 )

Output

[ ‘Indore’, ‘Bhopal’, ‘Jabalpur’ ]

3. A list can be created from mixed values like integers, floats, and strings.

list3 = [ 10, ‘Amit’, 15.7 ]

print( list3 )

Output

[ 10, ‘Amit’, 15.7 ]

4. We can also create an empty list. A list without any element is called an empty list.

list = [ ] 

print( list )

Output

[ ] 

5. We can create a list by concatenation of two or more lists with the + operator.

list1 = [ 10, 20, 30 ]

list2 = [ ‘Amit’, ‘Raju’ ] 

list3 = [ 3.5, 3.7 ]

list = list1 + list2 + list3

print( list )

Ouput

[ 10, 20, 30, ‘Amit’, ‘Raju’, 3.5, 3.7 ]

Important Links

If you like my post please share it with your friends by simply clicking the below social media button, and Don’t forget to comment so we will update ourselves.

Share in a Single Click

Leave a Reply

Your email address will not be published. Required fields are marked *