Python Lists

Python has 4 built-in data types that are used to store collections of data, They are Python Lists

  1. List
  2. Tuple
  3. Set
  4. Dictionary

In this tutorial we will learn about Lists.

In Python list is a built-in data-structure to store a collection of potentially dissimilar items. It offers operations similar to those of arrays, such as, indexing to access individual items. Unlike arrays, items can be of different data types and operations such as insert, delete, append are available.

A list is mutable, in that values of individual items can be changed at any time. It has additional methods, such as, search, sort. There are functions that can perform meaningful operations on a list, such as, len(), reversed() etc.

Creating a List

Creating Python List using [ ].

demo = []
print(demo)
print(type(demo))
[]
<class 'list'>

Simple Example of list

names = ["Surya", "Karthi", "Kamal hassan"]
print(names)
['Surya', 'Karthi', 'Kamal hassan']

Python List items are ordered, changeable, and allow duplicate values.

Example

names = ["Surya", "Karthi", "Kamal hassan", "Surya"]
print(names)
['Surya', 'Karthi', 'Kamal hassan', 'Surya']

In the above example we have same values ‘Surya’

demo = ["Surya", 25, "male", 5.7, "Kolkata"]
print(demo)
['Surya', 25, 'male', 5.7, 'Kolkata']

1. Accessing List items

We can access list items using their index. List indices start from zero .

demo = ["Surya", 25, "male", 5.7, "Kolkata"]

print(demo[0])
print(demo[1])
print(demo[2])
print(demo[3])
print(demo[4])
Surya
25
male
5.7
Kolkata

We can also use negative index to access the list values from the reverse order. we use the [::-1] to access in reverses the order.

demo = ["Surya", 25, "male", 5.7, "Kolkata"]

print(demo[-1])
print(demo[-2])
print(demo[-3])
print(demo[-4])
print(demo[-5])
Kolkata
5.7
male
25
Surya

2. Adding list items

For adding new elements to list we use insert() and append() methods.

insert()

Insert method takes two arguments. first is position of that item in list where you want to keep it and second one is that item.

list.insert(position,'item')

Example of insert() method.

demo = ["apple", "banana", "mango"]
demo.insert(0, "pineapple")

print(demo)
['pineapple', 'apple', 'banana', 'mango']

Append()

This function is used to modify an already existing list. Adds a new specific element at the end of the list.

demo = ["apple", "banana", "mango"]
demo.append("pineapple")
demo.append("guva")

print(demo)
['apple', 'banana', 'mango', 'pineapple', 'guva']

3. Deleting List items

del() Method is used to delete the items in the list permanently.

demo = ["apple", "banana", "mango"]
del demo[1]

print(demo)
['apple', 'mango']

4. Reverse List

reserve() Method is used to reverse the list items.

demo = ["apple", "banana", "mango"]
demo.reverse()

print(demo)
['mango', 'banana', 'apple']

List Methods

Here are some other common list methods.

  • list.append(element) — adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.
  • list.insert(index, element) — inserts the element at the given index, shifting elements to the right.
  • list.extend(list2) adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
  • list.index( element) — searches for the given element from the start of the list and returns its index. Throws a ValueError if the element does not appear (use “in” to check without a ValueError).
  • list.remove( element) — searches for the first instance of the given element and removes it (throws ValueError if not present)
  • list.sort() –– sorts the list in place (does not return it). (The sorted() function shown below is preferred.)
  • list.reverse() — reverses the list in place (does not return it)
  • list.pop(index) — removes and returns the element at the given index. Returns the rightmost element if index is omitted (roughly the opposite of append().

fullstackadda/pointsremember
  1. Python list is a built-in data-structure to store a collection of potentially dissimilar items.
  2. A list is mutable, in that values of individual items can be changed at any time.