Lists in Python

Python provides some in-built data structures which are extremely important while working in real-life applications. Lists are the primary data structures in python.

 The objects in the list may be of different types, it is not necessary to use the same data type objects while creating lists in python.

Lists are declared using square brackets [ ]. We can define lists of strings, numbers, or even lists of lists like 2d arrays in C++.

Example:

Letters = [ “a”, “b”, “c” ,“d” ,“e” ]
Numbers = [1,2,3,4,5]
Nested = [  [1,2,3,4,5],  [1,7,8,9,0] ]	

This nested list is a matrix. 

Python makes our development so easy that if we want to create a list of 100 ones we can do it simply using

Code:

Python Code

ones = [1] * 10
print(ones)

Output: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Concatenation of 2 lists

Just like the simple concatenation of 2 numbers, in python, we can even add 2 lists. 

Code:

Python Code

Letters = ["a","b","c","d","e"]
Numbers = [1,2,3,4,5]  
combined = Letters + Numbers
print(combined)

Output: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, 1, 2, 3, 4, 5]

Python list() function:

List functions take an iterable as an argument and we can generate a list of some defined range. Iterables are objects which are loop enabled i.e. we can iterate over them.

Code:

Python Code

numbers=list(range(10))
print(numbers)

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Strings are also iterable in python i.e. we can iterate over it. So we can generate a list out of strings also.

Code:

Python Code

str = list("takeuforward")
print(str)

Output:

[‘t’, ‘a’, ‘k’, ‘e’, ‘u’, ‘f’, ‘o’, ‘r’, ‘w’, ‘a’, ‘r’, ‘d’]

We can get the length of the string using len( )  function in python.

print(len(str)); // 12

Accessing items from the List

We can access items of a list using indexing as we do in arrays in C++/Java.

Code:

Python Code

numbers = [1,2,3,4,5]
print(numbers[0])
print(numbers[-1])

output:

1
5

If we pass -1 in the argument, it will return the last element from the list. This is called reverse indexing in python.

We can also modify the data in the list using square brackets.

Code:

Python Code

numbers = [1,2,3,4,5]
print(numbers[2])
numbers[2]=7
print(numbers[2])

Output:

3
7

Slicing a List

We can slice a list in python i.e. take out a fraction of the list from an existing list and put it into a new list.

Code:

Python Code

numbers = [1,2,3,4,5]
numbers[2]=7
list1=numbers[0:3]
#list1=numbers[:3] This will result in same output as 0 is the default argument to start.
print(list1)

Output: [1, 2, 7]

We can also add a step while slicing. This is very helpful while returning every 2nd or 3rd item.

Code:

Python Code

numbers = [1,2,3,4,5]
numbers[2]=7
list1=numbers[0::2]
print(list1)

Output: [1, 7, 5]

Note: Slicing never modifies the original list. It just returns a copy with the desired format.

Looping Over Lists

As told earlier, the lists are iterable i.e. we can iterate over them and that can be done using for( ) loop in python.

Code:

Python Code

numbers = [1,2,3,4,5]
for num in numbers:
	print(num)

Output:

1
2
3
4
5

Adding New Items to the List

We can add items to lists using the given methods in python. As everything in python is an object, we can access its methods using (.) DOT operator.

The method depends upon the position we want to add items.

list.append( item ) method adds an item at the end of the list.
list.insert( item ) method adds an item at the start of the list.

Removing an Item

list.pop() is used to pop the items from the list i.e from the back of the list.

We can also provide an index as an argument and it will delete the item at that index.

Sometimes we want to remove an item but don’t know the index so list.remove( item ) can be used to do so.

We can delete a range of items using del in python

numbers=[1,2,3,4,5,6]
del numbers[0:2]

Special thanks to Aryan Goyal for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article