Python Lists – basics: define, access, assign, delete, print, concatenate

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,…..]

Accessing elements of a list

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])

#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]);

Download the code Run the code

Assign values to list elements

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']

Download the code Run the code

Delete Elements From the List

To delete an element we use del() command as shown in the following example.

lst1 = ['Ajay', 'Bobby','Ashok','Vijay','Anil','Rahul','Alex','Christopher']

Download the code Run the code

Length of a List

To find out the length of a list we use len() function

lst1 = ['Ajay', 'Bobby','Ashok','Vijay','Anil','Rahul','Alex','Christopher']

Download the code Run the code

Printing all elements of a list

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

Concatenating two lists

There are multiple ways to concatenate two lists (or append one list to another).

  • The most basic way to concatenate two lists is using ‘+’ operator.
  • If you don’t want to use ‘+’ operator, you can call operator.add() function. Make sure you import operator module.
  • Lists can be concatenated by importing itertools module and then using the itertools.chain() method.
  • Lists can be also concatenated using extend() method.
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 = []
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.

Repetition of values in a list

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

Verify if an object exists in a list

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")

Download the code Run the code

<<< Python Arithmetic OperatorsPython Lists – Negative Indexing, Slicing, Stepping, Comparing, Max and Min >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .