Lists in Python
Lists in Python
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.
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.
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.
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.
# 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.