Python Tutorial: List in Python

List in python

This is the Python Tutorial: List in Python. In this Python Tutorial, you will learn the List in the python language. At the end of this tutorial, you will have full knowledge of List.

Hello, & Welcome!

Part 4- Python Tutorial: List in Python

In this article of Python Tutorial, you will learn following-

  1. How to create a List?
  2. Split a string into List?
  3. How to access an item in a List?
  4. Slicing in List?
  5. How to concatenate 2 List?
  6. Update items on the List?
  7. How to append in List?
  8. How to delete in List?
  9. Creating List inside List.

How to create a List?

In python, a list is created by using the Square bracket [] and put all item inside that bracket and separate them by comma (,).

The powerful feature of the list is that there is no size limit, and you can put different data type items in one list.

->You can also create an empty list.

For Example-

 #empty list
List = []    

#list with integer values
List = [1,2,3] 

#List with different Data type
List = [5, 7.9, "John"]    

Split a string into List?

We have covered this part in the previous tutorial of string. Let’s have a look in the example-

>>>List = "I am good man"
>>>List.split(' ')

OUTPUT-
['I','am','good','man']

How to access an item in a List?

-> Like in string, in List element index start from 0 to so on. If we want to access any particular item from the list, then we do the same job as done in the string.

>>>List = [1,2,3,4,5,6]
>>>List[2]

OUTPUT-
3

>>>List[5]
OUTPUT-
6

Access element via Negative Indexing-

In list, backward direction starts from -1,-2 and so on, same as in string. Let’s have a look-

>>>List = [1,2,3,4,5,6]
>>>List[-1]

OUTPUT-
6

Slicing in List?

Slicing means, cut some parts from the whole piece. The same thing, we can do with List in python.

If we want to get only a few items from a list, then we can perform slicing. The procedure is the same as we did in the string.

So, are you ready to know how to perform slicing? Let’s get started.

>>>List = [1,2,3,4,5,6,7,8]
>>>List[0:4]

OUTPUT-
[1,2,3,4]

-> It starts taking from index 0 to index 3, It doesn’t take element at index 4.

->I’ll show you how index numbers are assigned to each item.

>>>List = [1, 2, 3, 4, 5, 6, 7, 8]
    index= 0  1  2  3  4  5  6  7       

-> I hope, now you have a clear understanding of list indexing.

How to concatenate 2 List?

If you wanna add or concatenate two lists and make it one, so you can do it simply by using ‘+’ operator. It is as simple as that. Let’s have a look at an example.

>>>List1 = [1,2,3,4,5]
>>>List2 = [6,7,8,9,10]
>>>List3 = List1 + List2
>>>List3

OUTPUT-
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Update items on the List?

If you make a list and suddenly you have to change some items from the list, so you can do it very easily, by just passing an index value, on which index location you want to update with a new item.

Let’s see in the example-

>>>List=[1,2,3,4,5,6]
#change index 0 value with new value
>>>List[0]=9            
>>>List

OUTPUT-
[9,2,3,4,5,6]

How to append in List?

If you wanna add a new item in your current list, you can do it by using the append() method. Just pass the value in the append method, you wanna append in your list.

Let’s see in the example-

>>>List=[1,2,3,4,5,6]

#append element 7 in List[]
>>>List.append(7)
>>>List

OUTPUT-
[1,2,3,4,5,6,7]

How to delete in List?

If you don’t want to use some items in your list, then you can delete this item from the list, by using the pop() method. In the pop() method, you don’t have to need to pass the value, it deletes the last element from your list by default.

Let’s see in the example-

>>>List=[1,2,3,4,5,6]

#use pop() method to delete the last element in your list that is 6 in this List[].
>>>List.pop()
>>>List

OUTPUT-
[1,2,3,4,5]
  • But what?… If you wanna delete some specific items from your list that may be in between your list, don’t worry! For that task, python has one more method that is- remove()
  • By using remove() method, you can delete any item from your list, no matter its location in the list.

Let’s see in the example-

>>>List= [1,2,3,4,5,6,7]

#remove item 4 from your List
>>>List.remove(4)
>>>List

OUTPUT-
[1, 2, 3, 5, 6, 7]
  • There is one more problem, Guys!, If there are 2 same numbers in your list, and you wanna remove both numbers, then it fails.
  • This remove() method deletes only one item.

Let’s see in the example if there are 2 same numbers present in the list.

>>>List=[1,2,3,4,5,6,4]
>>>List.remove(4)
>>> List

OUTPUT-
[1, 2, 3, 5, 6, 4]
  • It deletes an item 4 only once, which comes first from index 0.

->Guys!… Don’t worry, there is one more trick to delete the item which you wanna delete, no matter how much time it repeats in a list.

->Suppose in List=[1,2,3,4,5,6,4,8,9], you don’t want to delete 4 at index 3, and you want to delete 4 which is at index 6, so you can use- del() method, which deletes an element of your choice.

Let’s see in the example-

>>>List=[1,2,3,4,5,6,4,8,9]
>>>del(List[6])
>>>List

OUTPUT-
[1,2,3,4,5,6,8,9]

->Syntax of del() method is- del(name of List[index position of item])

Creating List inside List.

If you wanna create a list inside another list, then you can do it by passing the list name to the new list, in which you wanna add.

Confused?… Let’s see in the example to make it more clear.

#List 1
>>>Colors=['red','green','yellow']

#List 2
>>>Fruits=['apple','banana','cherry']

#List 3 with List 1 and List 2
>>>num= [Colors,Fruits,[1,2,3,4,5]]
>>>num

OUTPUT-
[['red', 'green', 'yellow'], ['apple', 'banana', 'cherry'], [1, 2, 3, 4, 5]]
  • Here Colors is a list and Fruits is also a list, and we make one more list as num. In the num list, we pass both lists- Colors and Fruits and we pass num list own items. And all are added in one list as a num list.
  • As you can see in that example, in one list different data types of items are present. This the main feature of the list, that we can store different data type items in one list.

-> We can get a particular list from num list by writing following lines-

>>>num= [Colors,Fruits,[1,2,3,4,5]]
>>>num[0]

OUTPUT-
['red', 'green', 'yellow']    #which is List Colors at index 0

->We can get any particular element inside the list by writing following line-

>>>num= [Colors,Fruits,[1,2,3,4,5]]
>>>num[2][1]

OUTPUT-
2
  • In num[2][1]-
    • [2] represents the index location of the List.
    • [1] represents the index location of the element inside the list.

That’s all for List in Python, I hope now you have a better understanding of List.

congratulations! You successfully learned List in python.

Python Tutorial: Strings in Python

In the next tutorial, we will start learning Standard Input, Output in Python

Enjoy Learning Python!

Are you looking for Best Books on Python for Beginners and for Intermediate/Experts?

Check these Books, These Books are the best selling and covered everything in detail.

I hope you will find these Books helpful-

Best Selling Python Books for Beginners

Best Selling Python Books for Intermediates/Experts

All the Best!

Back to Python Tutorial

Thank YOU!

Though of the Day…

I The more I read, the more I acquire, the more certain I am that I know nothing.’ ”

– Voltaire

Leave a Comment

Your email address will not be published. Required fields are marked *