LISTS



Lists - Python


List is a collection which is ordered and changeable. Allows duplicate members.


Create a List:

Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> list=["apple","ball","cat","dog"]
>>> print(list)
['apple', 'ball', 'cat', 'dog']


Access Items in the List :

You access the list items by referring to the index number ( indexing starts from o ) :


Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>>list=["apple","car","dog","gear"]
>>>print(list[1])
car

Negative Indexing :

Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.


Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>>list=["apple","car","dog","gear"]
>>>print(list[-2])
dog

Range of Indexes :

You can specify a range of indexes by specifying where to start and where to end the range.When specifying a range, the return value will be a new list with the specified items.


Return the third, fourth, and fifth item -

Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>>list=["apple","banana","melon","apricot","kiwi"]
>>>print(list[2:5])
>>>['melon','apricot','kiwi']

Note: The search will start at index 2 (included) and end at index 5 (not included).


Range of Negative Indexes :

Negative indexes starts the search from end of the list.

This example returns the items from index -4 (included) to index -1 (excluded)

>>>list=["apple","banana","melon","apricot","kiwi"]
>>>print(list[-4:-1])
['banana','melon','apricot']

Change Item Value :

To change the value of a specific item, refer to the index number.

Change the second item:

>>>list=["apple","banana","melon","apricot","kiwi"]
>>>list[1]="black current"
>>>print(list)
['apple','black current','melon','apricot','kiwi']

Loop Through a List :

You can loop through the list items by using a for loop.

Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> x=["paul",2016,3.6]
>>> for i in x:
	  print(i)

paul
2016
3.6


Check if Item Exists :

To determine if a specified item is present in a list use the in keyword.

Check if "apple" is present in the list:

>>>list=["apple","ball","cat","dog"]
>>>if "apple" in list:
>>>("yes, 'apple' is in list")

"yes, 'apple' is in list"


List Length :

To determine how many items a list has, use the len() method.

Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

>>> list=["apple","ball","cat","dog"]
>>> print(len(list))
4

Add Items :

To add an item to the end of the list, use the append() method.


Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

>>> list=["good","sad","bad","better"]
>>> list.append("best")
>>> print(list)
['good', 'sad', 'bad', 'better', 'best']


Insert Items :

To add an item at the specified index, use the insert() method.


Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

>>> list=["pen","pencil","eraser","sharpener"]
>>> list.insert(1,"scale")
>>> print(list)
['pen', 'scale', 'pencil', 'eraser', 'sharpener']


Remove Item :

To remove items from the list various methdos are used:


1. The remove() method removes the specified item

Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> list=["apple","ball","cat","dog"]
>>> list.remove("ball")
>>> print(list)
['apple', 'cat', 'dog']


2. The pop() method removes the specified index, (or the last item if index is not specified):

>>>list=["apple","ball","cat","dog"]
>>>list.pop(1)
>>>'ball'
>>>list.pop()
>>>'dog'

3. The del keyword removes the specified index and also deletes the entire list.

>>> list=["mom","dad","son","daughter"]
>>> del list[0]
>>> print(list)
['dad', 'son', 'daughter']

>>>list=["apple","ball","cat","dog"]
>>>delete list
SyntaxError: invalid syntax
print(list) #this will cause an error because you have succsesfully deleted "list".




4. The clear() method empties the list:

>>> list=["plates","glasses","spoons"]
>>> list.clear()
>>> print(list)
	  
[]
>>> 



Copy a List :

We cannot copy a list simply by typing list2 = list1 because, list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2.
There are ways to make a copy, one way is to use the built-in List method copy().


Make a copy of a list with the copy() method.


>>> list1=["apple","banana","cherry","strawberry"]
>>> list2=list1.copy()
>>> print(list2)
['apple', 'banana', 'cherry', 'strawberry']


Another way to make a copy is to use the built-in method list().


>>>thislist = ["apple", "banana", "cherry"]
>>>mylist = list(thislist)
>>>print(mylist)
['apple','banana','cherry']


Join Two Lists :

There are several ways to join, two or more lists in Python.
One of the easiest ways are by using the + operator.


>>> list=["a","b","c"]
>>> list1=[1,2,3]
>>> listnew=list+list1
>>> print(listnew)
['a', 'b', 'c', 1, 2, 3]


Another way to join two lists are by appending all the items from list2 into list1, one by one.


>>> list1=[1,2,3]
>>> list2=["a","b","c"]
>>> for i in list2:
	  list1.append(i)
	  print(list1)

[1, 2, 3, 'a']
[1, 2, 3, 'a', 'b']
[1, 2, 3, 'a', 'b', 'c']


We can use the extend() method, which purpose is to add elements from one list to another list.


>>> list2=[2.5,3.5,6.5]
>>> list1=[2,3,4]
>>> list1.extend(list2)
>>> print(list1)
[2, 3, 4, 2.5, 3.5, 6.5]


The list() Constructor :

It is also possible to use the list() constructor to make a new list.


>>>thislist=(("apple","ball"))
>>>print(thislist)
('apple','ball')

Python List Built-in functions :


S.no.FunctionDescription
1cmp(list1,list2)compares the elements of both the lists
2len(list)It is used to calculate length of the list
3max(list)It returns maximum elements of the list
4min(list)returns the minimum elements of the list
5list(seq)converts any sequence to the list

Python List built-in methods :


S.no.MethodDescription
1append()Adds an element at the end of the list
2clear()Removes all the elements from the list
3copy()Returns a copy of the list
4count()Returns the number of elements with the specified value
5extend()Add the elements of a list (or any iterable), to the end of the current list
6index()Returns the index of the first element with the specified value
7insert()Adds an element at the specified position
8pop()Removes the element at the specified position
9remove()Removes the item with the specified value
10reverse()Reverses the order of the list
11sort()Sorts the list


RegEx SETS

Python - RegEx SETS

posted on 2019-11-12 23:06:24 - Python Tutorials


RegEx_Functions

Python - RegEx_Functions

posted on 2019-11-09 06:07:29 - Python Tutorials


RegEx_Sets

Python - RegEx_Sets

posted on 2019-11-09 05:30:54 - Python Tutorials


Prompt Examples

ChatGPT Prompt Examples

posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


Use Cases

Chat GPT Key Use Cases

posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


Prompt Frameworks

Prompt Frameworks

posted on 2023-06-21 19:33:06 - ChatGPT Tutorials