What are lists in Python? Complete guide with examples
In Python, when you want to store multiple items in a single variable, then Lists are used. It is used to store the collection of data. It comes in under one of the four built-in data types -Tuple, Dictionary, and Set. These all have different usage and quality. Think of Lists as dynamic-sized arrays used in different languages such as – C++(vector), and Java(ArrayList). All of the things in the Lists are enclosed within square brackets – []
, and is separated using commas ,
.
Creating a List
Here’s how a List is created in Python-
Var = ["One", "Two", "Three"]
print(Var)
Code language: Python (python)
Output:
["One", "Two", "Three"]
Code language: Python (python)
Lists are an integral part of Python language. These are the simplest containers. It is a very powerful tool because it need not be homogeneous always. The List named Var
above can contain Integers, Objects, and Strings as well. Lists can be altered even after their creation i.e. they are mutable. Lists do not need any built-in function for their creation.
Accessing elements from the List
If you want to access the element from the List, you need to use indexes that must be an integer. If our List is nested then we can use nested indexing.
List = ["One", "Two", "Three"]
# accessing a element from the
# list using index number
print("Accessing a element from the list")
print(List[0])
print(List[2])
Code language: Python (python)
Accessing a element from the list
One
Three
Code language: Python (python)
Negative indexing
When you use negative indexes in Python, they will represent positions from the end of the array. You do not need to write any offset for this, writing List[-3]
would be enough. Negative will bring us to the end, so -1 will access the last element, -2 will access the second last element, and so on.
List = [1, 2, 'Hello', 4, 'Hi', 6, 'Hey']
# accessing an element using
# negative indexing
print("Accessing element using negative indexing")
# printing the last element of list
print(List[-1])
# printing the 3rd last element of list
print(List[-3])
Code language: Python (python)
#Output
Accessing element using negative indexing
Hey
Hi
Code language: Python (python)
Size of Python list
In python, we have a len()
method that is used to get the length of the List.
# Creating a List
List1 = []
print(len(List1))
# Creating a List of numbers
List2 = [1, 2, 3]
print(len(List2))
Code language: Python (python)
#Output
0
3
Code language: Python (python)
Input of a Python List
In Python, the default input is String, but we can take any type of input for List, it can be an integer, float, etc.
string = input("Enter elements")
lst = string.split()
print('The output is:', lst)
Code language: Python (python)
Output:
Enter elements: One Two Three
The output is: ['One', 'Two', 'Three']
Code language: Python (python)
Adding Elements to a Python List
We can also add or append elements to the List by using various methods-
append() method
append()
is a built-in function in Python. We can add elements to our List using this method, one element at a time can be added. If you want to add multiple elements at a time, then you can use a loop. Since Tuples are immutable, they can also be added to the Lists by using the append()
method. We can also add a List to the existing List, which is not the case with Sets.
List = []
print("Initial blank List: ")
print(List)
# Addition of Elements
# in the List
List.append(1)
List.append(2)
List.append(4)
print("\nList after Addition of Three elements: ")
print(List)
# Adding elements to the List
# using Iterator
for i in range(1, 4):
List.append(i)
print("\nOutput after Addition of elements from 1 to 3: ")
print(List)
# Adding Tuples to the List
List.append((5, 6))
print("\nList after Addition of a Tuple: ")
print(List)
# Addition of List to a List
List2 = ['One', 'Two']
List.append(List2)
print("\nList after Addition of a List: ")
print(List)
Code language: Python (python)
#Output
Initial blank List:
[]
List after Addition of Three elements:
[1, 2, 4]
Output after Addition of elements from 1 to 3:
[1, 2, 4, 1, 2, 3]
List after Addition of a Tuple:
[1, 2, 4, 1, 2, 3, (5, 6)]
List after Addition of a List:
[1, 2, 4, 1, 2, 3, (5, 6), ['One', 'Two']]
Code language: Python (python)
Using insert() method
By using append()
method, elements get added to the end of the List. If you want to insert the element at your desired position, then insert()
method can be used. It requires two arguments- (position, value)- position means the index at which you want to insert the element, and value is just the element you want to add.
List = [1,2,3,4]
print("Initial List: ")
print(List)
# Addition of Element at
# specific Position
# (using Insert Method)
List.insert(3, 12)
List.insert(0, 'Hello')
print("\nList after performing Insert Operation: ")
print(List)
Code language: Python (python)
#Output
Initial List:
[1, 2, 3, 4]
List after performing Insert Operation:
['Hello', 1, 2, 3, 12, 4]
Code language: Python (python)
Using extend() method
If you want to add multiple elements at the end of the List, then extend()
method can be used. Just like append()
method, this method can only add the element at the end of the List.
List = [1, 2, 3, 4]
print("Initial List: ")
print(List)
# Addition of multiple elements
# to the List at the end
# (using Extend Method)
List.extend([8, 'Hi', 'Always'])
print("\nList after performing Extend Operation: ")
print(List)
Code language: Python (python)
#Output
Initial List:
[1, 2, 3, 4]
List after performing Extend Operation:
[1, 2, 3, 4, 8, 'Hi', 'Always']
Code language: Python (python)
Reversing a List
In Python, we have a reverse()
method, that can be used to reverse the List.
mylist = [1, 2, 3, 4, 5, 'Hello', 'Python']
mylist.reverse()
print(mylist)
Code language: Python (python)
['Python', 'Hello', 5, 4, 3, 2, 1]
Code language: Python (python)
The output will be the reverse of the input.
Conclusion
In this article, we discussed a complete guide about the Python List.
I hope you have understood the concept.
Thanks for reading!
Sharing is caring
Did you like what Vineeta Tiwari wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses:
302 students learning
Haris
Python Crash Course for Beginners
Surendra varma Pericherla
Learn Data Structures Using Python