Python Lists :

Python Collections (Arrays) :

There are four collection data types in the Python programming language :

  1. List is a collection which is ordered and changeable. Allows duplicate members.
  2. Tuple is a collection which is ordered and changeable. Allows duplicate members.
  3. Set is a collection which is ordered and changeable. Allows duplicate members.
  4. Dictionary is a collection which is ordered and changeable. Allows duplicate members.

A list is a sequence :

Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in a list are called elements or sometimes items. There are several ways to create a new list; the simplest is to enclose the elements in square brackets ( [ and ] ): 1. [10,20,30,40] 2. ["your name","your age","address"]
The first example is a list of four integers. The second is a list of three strings. The elements of a list don’t have to be the same type. python also have nested list feature (A list within another list is nested) : ["ayan",1,["age",24]]


Creating and Accessing Values in list :

Creating a list is as simple as putting different comma-separated values between square brackets. To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.

Example :

  1. ourlist = ["ayan","rayan","apu","dev"]
  2. print(ourlist) #print full list
  3. print(ourlist[1]) #Print the second item of the list (start with 0)
  4. ourlist[1]="symul" #Change / Updating the second item
  5. print(ourlist) #print updated list items
  6. input()

Output :

python list examples

Loop Through a List :

You can loop through the list items by using a for loop:

Example :

  1. ourlist = ["ayan","rayan","apu","dev"]
  2. for x in ourlist : print(x) #Print all items in the list, one by one
  3. input()

Output :

python list with for loop

Check if Item Exists :

To determine if a specified item is present in a list use the in keyword:

Example :

  1. ourlist = ["ayan","rayan","apu","dev"]
  2. if "ayan" in ourlist : print("Yes, 'ayan' is in the list") #Check if "ayan" in the list
  3. input()

Output :

python list with if statement

List with methods :

python list have built-in methods, methods help us to manipulate list items.

Example :

  1. ourlist = ["ayan","rayan","apu","dev"]
  2. ourlist.append("rayan") #Using the append() method to append/adding an item
  3. print(ourlist)
  4. print(len(ourlist)) #Print the number of items in the list
  5. ourlist.insert(1,"alex") #Insert an item as the second position
  6. print(ourlist)
  7. ourlist.remove("rayan") #The remove() method removes the specified item
  8. print(ourlist)
  9. input()

Output :

python list with methods

List methods :

Python includes many built-in methods but here we just adding some of the following built-in methods to manipulate lists :

  • The len() method determine how many items a list has.
  • The append() method add an item to the end of the list.
  • The insert() method add an item at the specified index.
  • The remove() method are several methods to remove items from a list.
  • The pop() method removes the specified index, (or the last item if index is not specified).
  • The remove() method are several methods to remove items from a list.
  • The reverse() method are reverses the order of the list.
  • The sort() method are sorts the list.

Next Update

Loading...

Currently we added Python Language Tutorials, more new updates coming soon.

We make a Collaboration with HackIsOn YouTube Channel So, Please Subscribe Their Channel and keep supporting