Python lists allow you to hold several objects in an order. Every element is assigned an index which indicates its position. The first element in a list is assigned an index value of zero (0), the second element is at position 1 and so on.
A list object can be created in a very simple manner. All values inside the list are written in square brackets and is separated by a comma. The list can be created as shown below:
lst1 = [] lst2 = [item1,item2,item3,…..]
Suppose you have a list of country names as shown below and you want to print the value of the second element in the list, you need to use index=1 since Python lists start with index=0. You just need to type: print (lst1[1])
list1=['India','America','Japan','Sweden','China','Poland'] #printing element of a list at position 2 print('We will now try to print the element at second position in the list'); print('element at position 2 is : '+ list1[1]); print('***');
Download the code Run the code
The elements of the list can be accessed and updated. You can assign a new value to any element of a list. Consider the below list :
lst1 = ['Ajay', 'Bobby','Ashok','Vijay','Anil','Rahul','Alex','christopher'] print(lst1[3]); lst1[3]='Jackie'; print(lst1[3]);
Download the code Run the code
To delete an element we use del() command as shown in the following example.
lst1 = ['Ajay', 'Bobby','Ashok','Vijay','Anil','Rahul','Alex','Christopher'] print(lst1[3]); del(lst1[3]); print(lst1[3]);
Download the code Run the code
To find out the length of a list we use len() function
lst1 = ['Ajay', 'Bobby','Ashok','Vijay','Anil','Rahul','Alex','Christopher'] print(len(lst1));
Download the code Run the code
All elements of a list can be printed using ‘for’ command
lst1 = ['Ajay', 'Bobby','Ashok','Vijay','Anil','Rahul','Alex','Christopher'] For x in lst1: print(x)
Download the code Run the code
There are multiple ways to concatenate two lists (or append one list to another).
lst1 = ['Ajay', 'Bobby']; lst2 = ['Rahul','Alex'] print("\nConcatenate using + operator") lst3 = lst1+lst2 for x in lst3: print(x) # using + operator print("\nConcatenate using operator.add() function") import operator for item in operator.add(lst1, lst2): print (item) #using operator.add() method print("\nConcatenate using itertools.chain() function") import itertools for item in itertools.chain(lst1, lst2): print (item) # using itertools print("\nConcatenate using extend() function") lst4 = [] lst4.extend(lst1) lst4.extend(lst2) for item in lst4: print (item) # using extend() method
Download the code Run the code
This stackoverflow discussion has more information about concatenating lists in python.
You can use ‘*’ operator on the list to repeat all the values as many times you want
lst1 = ['Ajay', 'Bobby','Ashok','Vijay','Anil','Rahul','Alex','Christopher'] lst2 = lst1 * 2 for x in lst2: print(x)
Download the code Run the code
You can verify whether an object exists in a list or not by using an ‘if’ command. If yes, it returns true or else it will return false.
lst1 = ['Ajay', 'Bobby','Ashok','Vijay','Anil','Rahul','Alex','Christopher'] if 'Ajay' in lst1: print("true")