All IT Courses 50% Off
Python Tutorials

Understanding Python Lists

Lists are considered as dynamically sized arrays that will be used which are utilized in another language. Lists don’t need to be homogeneous always, which makes it the most powerful tool in python. A single list consists of data types like integers, strings, and objects. Lists are mutable and can be altered even after their creation.

Lists in python will be ordered and have a definite count. The elements in a list are indexed as per definite sequence and indexing of a list is done with 0 being the first index. Each element in the list has its definite place in the list where it is allowed to duplicate elements in a list with each element which is having its own distinct place and also credibility.

Creating a list

The lists in python can be built by just placing sequencing the square brackets[]. Lists have mutable elements. Consider the example

Python program to demonstrate
# Creating a ListList = []print(“Blank List: “)
print(List) 
# Creating a List of numbers
List = [10, 20, 14]print(“\nList of numbers: “)
print(List) 
# Creating a List of strings and accessing
# using index
List = [“Hello”, “For”, “people”]print(“\nList Items: “)
print(List[0])print(List[2]) 
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [[‘Hello’, ‘For’], [‘people’]]print(“\nMulti-Dimensional List: “)
print(List)

Output: 

Blank List: 

[]

List of numbers: 

[10, 20, 14]

List Items

Hello

people

Multi-Dimensional List: 

[[‘Hello’, ‘For’], [‘people’]]

Adding the elements to the list

By using the append() method

The elements that may be added to the list using the built-in append() function. Only one element that time added to the list by using the append method for any addition of multiple elements with append() method, loops will be used, Tuples that can also be included to the list with the use of the append method because the tuples are immutable. Lists that can be added to the existing list with the use of the append() method. For example

Python program to demonstrate
# Addition of elements in a List
 # Creating a List
List = []print(“Initial blank List: “)
print(List) 
# Addition of Elements in the List
List.append(1)
List.append(2)
List.append(4)
print(“\nList whereafter Addition of Three elements: “)
print(List) 
# Adding elements to the List
# using Iterator
for i in range(1, 4):    
List.append(i)
print(“\nList whereafter Addition of elements from 1-3: “)
print(List) 
# Adding Tuples to the List
List.append((6,5))
print(“\nList whereafter Addition of a Tuple: “)
print(List) 
# Addition of a  ListL
ist2 = [‘For’, ‘people’]List.append(List2)
print(“\nList whereafter Addition of a List: “)
print(List)

Output: 

Initial blank List: 

[]

List whereafter Addition of Three elements: 

[1, 2, 4]

List whereafter Addition of elements from 1-3: 

[1, 2, 4, 1, 2, 3]

List whereafter Addition of a Tuple: 

[1, 2, 4, 1, 2, 3, (6,5)]

List whereafter Addition to List: 

[1, 2, 4, 1, 2, 3, (6, 5), [‘For’, ‘people’]]

By using the insert() method

The append () method does the addition of elements at the last end of the list, for the addition of elements at the required position, the insert() method is used. Append() which takes only one argument, the insert() method requires two arguments(position,value). For example

Python program to demonstrate
# Addition of elements in a List  
# Creating a List
List = [1,2,3,4]print(“Initial List: “)
print(List) 
# Addition of Element at specific Position
# (By using Insert Method)
List.insert(3, 12)
List.insert(0, ‘people’)
print(“\nList whereafter performing Insert Operation: “)
print(List)

Output: 

Initial List: 

[1, 2, 3, 4]

List whereafter performing Insert Operation: 

[‘people’, 1, 2, 3, 12, 4]

Using the extend () method

Apart from append() and insert() method there’s one more method we have for the addition of elements, extend(), this method will be used to add the multiple elements at the end.

For example
Python program to demonstrate
# Addition of elements in a List 
# Creating a 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, ‘people’, ‘Always’])
print(“\nList whereafter performing Extend Operation: “)
print(List)

Output: 

Initial List: 

[1, 2, 3, 4]

List whereafter performing Extend Operation: 

[1, 2, 3, 4, 8, ‘people’, ‘Always’]

 Questions

  1. What are Lists in Python how it is used?

2. Explain the insert method with an example?

Facebook Comments

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button

Get Python Course
worth 499$ for FREE!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys Blog will use the information you provide on this form to be in touch with you and to provide updates and marketing.