Lists in Python

shankar saripalli
3 min readFeb 27, 2021
  1. A list is a data structure in Python that is a mutable, ordered sequence of elements.

Just as strings are defined as characters between quotes, lists are defined by having different data types between square brackets [ ]

Cars=[123,'Audi','BMW']

2. Mutable means that you can change the items inside, while ordered sequence is in reference to index location.

Lets Change the first item in the list as shown below.We can see that the first item has changed from 123 to ‘Mercedes’

Cars=[123,'Audi','BMW']Cars[0]='Mercedes'print(Cars)['Mercedes', 'Audi', 'BMW']

3. The first element in a list will always start at index 0

Cars.index('Mercedes')
0

4. Each element or value that is inside of a list is called an item

5. Like strings, each item within a list is assigned an index, or location, for where that item is saved in memory

6. Lists are also known as a data collection. Data collections are simply data types that can store multiple items

7. You can make a list that includes the letters of the alphabet, the digits from 0–9, or the names of all the people in your family. You can put anything you want into a list, and the items in your list don’t have to be related in any particular way

L=[1,9.0,'Alex']

Lets deep dive into List’s and its operations

  1. Accessing Elements in a list : We can access the items in a list using the index position
Cars=['Mercedes','Audi','BMW']
print(Cars[0])
'Mercedes'

2. Appending Elements to list: The simplest way to add a new element to a list is to append the item to the list. When you append an item to a list, the new element is added to the end of the list

Cars=['Mercedes','Audi','BMW']
Cars.append('Volvo')
print(Cars)
['Mercedes', 'Audi', 'BMW', 'Volvo']

The append() method makes it easy to build lists dynamically. Lets take a empty list names ‘Bikes’ and add elements to it

Bikes=[]
Bikes.append('Honda')
Bikes.append('Yamaha')
Bikes.append('Bajaj')
print(Bikes)
['Honda', 'Yamaha', 'Bajaj']

3. Inserting Elements into a List: You can add a new element at any position in your list by using the insert() method. You do this by specifying the index of the new element and the value of the new item.

Cars.insert(0,'Porsche')
print(Cars)
['Porsche', 'Mercedes', 'Audi', 'BMW', 'Volvo']

4. Removing an Item Using the del Statement:If you know the position of the item you want to remove from a list, you can use the del statement.

del Cars[0]
print(Cars)
['Mercedes', 'Audi', 'BMW', 'Volvo']

5. Removing an Item Using the pop() Method: The pop() method removes the last item in a list

Cars=['Mercedes','Audi','BMW']
Cars.pop()
print(Cars)
['Mercedes', 'Audi', 'BMW']

6. Popping Items from any Position in a List: You can use pop() to remove an item from any position in a list by including the index of the item you want to remove in parentheses.

Cars=['Mercedes','Audi','BMW','Volvo']
Cars.pop()
print(Cars)
['Mercedes', 'Audi', 'BMW']

7. Removing an Item by Value: Sometimes you won’t know the position of the value you want to remove from a list. If you only know the value of the item you want to remove, you can use the remove() method

Cars=['Mercedes','Audi','BMW']
Cars.remove('BMW')
print(Cars)
['Mercedes', 'Audi', 'Volvo']

8. Sorting List : Python’s sort() method makes it relatively easy to sort a list

Cars=['Mercedes','Audi','BMW','Volvo']
Cars.sort()
print(Cars)
['Audi', 'BMW', 'Mercedes', 'Volvo']

9. Sorting using sorted function :Use sorted function to maintain the original order in the list but to present in sorted order

Cars=['Mercedes','Audi','BMW','Volvo']
print(sorted(Cars))
print(Cars)
['Audi', 'BMW', 'Mercedes', 'Volvo']
['Mercedes', 'Audi', 'BMW', 'Volvo']

10. Reverse the list : To reverse the original order of a list, you can use the reverse() method

Cars=['Mercedes','Audi','BMW','Volvo']
Cars.reverse()
print(Cars)
['Volvo', 'BMW', 'Audi', 'Mercedes']

11. Length of list : You can quickly find the length of a list by using the len() function

Cars=['Mercedes','Audi','BMW','Volvo']
print(len(Cars))
4

12. Common Errors while working with Lists:Let’s say you have a list with three items, and you ask for the fourth item then it throws error as shown below

WHEN TO USE LISTS

1. If you need to maintain order. Remember, this is listed order, not sorted order. Lists do not sort for you.

2. If you need to access the contents randomly by a number. Remember, this is using cardinal numbers starting at 0

--

--