0% found this document useful (0 votes)
0 views6 pages

Lists in Python

Python Lists are dynamic arrays that can store a collection of data types, including integers, strings, and objects, and they are mutable. Lists can be created using square brackets or the list() constructor, and they can contain duplicate values. Accessing elements is done using index numbers, and negative indexing allows for accessing elements from the end of the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

Lists in Python

Python Lists are dynamic arrays that can store a collection of data types, including integers, strings, and objects, and they are mutable. Lists can be created using square brackets or the list() constructor, and they can contain duplicate values. Accessing elements is done using index numbers, and negative indexing allows for accessing elements from the end of the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lists

Python Lists are just like dynamically sized arrays.


The list is a sequence data type which is used to store the collection of data.
In simple language, a list is a collection of things, enclosed in [ ] and separated by
commas.
Lists are the simplest containers that are an integral part of the Python language. Lists
need not be homogeneous always which makes it the most powerful tool in Python. A
single list may contain Datatypes like Integers, Strings, as well as Objects. Lists are
mutable, and hence, they can be altered even after their creation.

(Define from Motwani Book)

Lists can be created using the list object known as “list”.


Let’s get help on list object using the “help” function.

Code1:
>> Help(list)

Lists in Python can be created by just placing the sequence of elements inside the
square brackets [].

Code2:
name_list = ['Pruthvi', 'Raja', 'Python', 'Welcome', 1, 2, 3, 4, 5]
print(name_list)
print(type(name_list))

Before we can work with the lists, it is important to know the number of attributes and
methods available for lists using the well known “dir” method.
The dir() function returns all properties and methods of the specified object,
without the values. This function will return all the properties and methods, even
built-in properties which are default for all object.
Code3:
Print(dir(list))
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__',
'__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__',
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
'__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop',
'remove', 'reverse', 'sort']

Here we can see the list of available attributes and methods on lists.

Now, let’s begin our journey on lists by creating an empty list.


An empty list can be created using the empty [ ] without any values or elements.

Code4:
List = [ ]
print("Blank List: ")
print(List)

# Creating a List of elements with mixed type elements or values. These are also
known as heterogeneous data elements.

Code5:
name_list = ['Pruthvi', 'Raja', 'Python', 'Welcome', 1, 2, 3, 4, 5]
print("List of elements: ")
print(name_list)

A list can contain unlimited data depending upon the limitation of your computer's
memory.
For example:

Code6:
print(list(range(10000)))

In the above example I have used list a built in function to create a list with the range()
object. It means we can create a list with the list object by passing an iterable object as
the parameter.
A list may contain duplicate values with their distinct positions and hence, multiple
distinct or duplicate values can be passed as a sequence at the time of list creation.

# List may contain duplicate elements or repeated elements


Code7:
rep_list = ['Pruthvi', 'Raja', 'Python', 'Welcome', 'Welcome', 1, 2, 3, 4, 5, 5]
print("List with repeated items:")
print(rep_list)

Multi-ways to create a list:

All the list objects are the objects of the list class in Python. Use the list() constructor to
convert from other sequence types such as tuple, set, dictionary, string to list.
# Multi-ways to create lists
# All the list objects are the objects of the list class in Python. Use the list() constructor
to convert from other sequence types such as tuple, set, dictionary, string to list.

#list from homogeneous or heterogeneous elements


nums=[1,2,3,4]
print(type(nums))

# creating lists from the string


mylist=list('Hello')
print(mylist)

# creating list from the dictionary


nums=list({1:'one',2:'two'})
print(nums)

# list from the tuple


nums=list((10, 20, 30))
print(nums)

# list from the set


nums=list({100, 200, 300})
print(nums))
Before we can see the built in attributes and methods on lists, it is important to see
some of the very important operations and Python built in functions on lists.

Let’s begin with the Accessing elements from the List.

In order to access the list items refer to the index number. Use the index operator [ ] to
access an item in a list. The index must be an integer.

Code:
name_list = ['Hello', 'Pruthvi', 'Raja', 'Welcome', 'To', 'Python', 'Tutorial']
print('name_list:\n', name_list, sep='')
print("\nList Items: ")
print(name_list[0])
print(name_list[1])

Nested lists are accessed using nested indexing. The nested lists are also called as
multi-dimensional lists.

# Nested lists or multi-dimensional lists


nested_list_1d = ['Pruthvi', 'Raja', 'Python', 'Welcome', 'Welcome', 1, 2, 3, 4, 5, 5]
print(type(nested_list_1d))
nested_list_2d = [['Pruthvi', 'Raja'], 'Python', 'Welcome', 'Welcome', 1, 2, 3, 4, 5, 5]
print(type(nested_list_2d))
nested_list_3d = [[['Pruthvi', 'Raja'], ['Python', 'Welcome']], 'Welcome', 1, 2, 3, 4, 5, 5]
print(type(nested_list_3d))

# Let’s access the multi-dimensional lists using the index method


# Accessing multi-dimensional lists
sublist_2d = nested_list_2d[0]
print(sublist_2d)
print(type(sublist_2d))
print(sublist_2d[0])
print(sublist_2d[1])
print(type(sublist_2d[0]))
print(nested_list_2d)
print(nested_list_2d[0][0])
print(nested_list_2d[0][1])

# Similarly, we can access the 3d list elements using the index method
Negative indexing

In Python, negative sequence indexes represent positions from the end of the array.
Instead of having to compute the offset as in List[len(List)-3], it is enough to just write
List[-3]. Negative indexing means beginning from the end, -1 refers to the last item, -2
refers to the second-last item, etc.

You might also like