Mindmajix

Lists concepts in Python

Lists

Python allows you to hold a number of objects in an order in the list. A List is like a container that can hold objects in a sequence and allows you to add or delete objects from the sequence.

Lists are what they seem – a list of values. Each one of them is numbered, starting from zero – the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end.

Lists are extremely similar to tuples. Lists are modifiable (or ‘mutable’, as a programmer may say), so their values can be changed. Most of the time we use lists, not tuples, because we want to easily change the values of things if we need to.

Creating a list is a very simple. The expression is as follows:

myList = []

myList = [obj1,obj2,obj3,…..]

Python also provides support for computed lists. This is called “list comprehensions”, which allows evaluation of expression at least once for each element in the sequence. The expression for list comprehension is as follows:

myList =[to_do for every item in the sequence]

Where to_do is the expression that is to be repeated for each element.

ACCESSING LIST

To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index. Lists implement the standard sequence interface.

1. To know the number of items in a list:

Format:

Num=len(list_name)

EXAMPLE:

myList =[‘Sunday’,’Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’,’Saturday’]

len(myList)

Output: 7

2. To know the value of the element at index ‘i’

Format:

Element=list_name[i]

EXAMPLE:

myList[2]

Output: ‘Tuesday’

3. To know the values of elements between index ‘i’ and ‘j’

Format:

Val=list_name[i:j]

EXAMPLE:

myList[2:5]

Output: [‘Tuesday’,’Wednesday’,’Thursday´]

LOOPS

1. To print items in a list:

Format:

For item in list_name:

print item

EXAMPLE:

for element in myList: print element

Output: Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday

2. To print both index and item in a list:

Format:

for index, element in enumerate(list_name):

print index, element

EXAMPLE:

>>> for index, element in enumerate(myList):

print index, element

Output:

0Sunday;1Monday;2Tuesday;3Wednesday;4Thursday;5Friday;6 Saturday

3. You can also use the built in iter function

Format:

i=iter(list_name)

item = i.next()#this will fetch the first value

EXAMPLE:

  • i=iter(myList)
  • item=i.next()

>>> print item

Output: Sunday

MODIFYING LISTS

The list type also allows you to assign to individual items or slices, and to delete them.

1. To add a new element to the list

Format:

list_name.append(element)

EXAMPLE:

  • append(‘no day’)
  • print myList

Output: [‘Sunday’,‘Monday’,‘Tuesday’,‘Wednesday’,‘Thursday’,‘Friday’,‘Saturday’,‘no day’]

2. To add items of another list

Format:

list_name.extend(list_name2)

EXAMPLE:

  • myList =[‘Sunday’,’Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’,’Saturday’]
  • myListMonth= [‘January’,’February’,’March’,’April’,’May’,’June’,’July’]
  • extend(myListMonth)
  • myList

Output: [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘Januar

3. To insert an element at a particular index

Format:

list_name.insert(index,element)

EXAMPLE:

  • insert(7,’now months’)
  • myList

Output:

[‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘now-months’, ‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’]

4. Remove an individual item

Format:

del list_name[index]

EXAMPLE:

  • del myList[7]
  • myList

Output: [‘Sunday’,’Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’,’Saturday’, ‘Januar

5. Remove sequence of items from the list

Format:

del list_name[i:j]

EXAMPLE:

  • del myList[7:9]
  • myList

Output: [‘Sunday’,’Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’,’Saturday’,’March’

6. Remove an item and return it

Format:

item=list_name.pop() #for removing last item item=list_name.pop(index)#for removing item at particular point

EXAMPLE 1:

  • item=myList.pop()
  • item

Output: ‘July’

>>> myList

Output: [‘Sunday’,’Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’,’Saturday’,’March’

EXAMPLE 2:

  • item=myList.pop(7)
  • item

Output: ‘March’

>>> myList

Output: [‘Sunday’,’Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’,’Saturday’,’April’,’

7. Reverse the order of the list

Format:

myList.reverse()

EXAMPLE:

  • reverse()
  • myList

Output: [‘June’,’May’,’April’,’Saturday’,’Friday’,’Thursday’,’Wednesday’,’Tuesday’,’Mond

Enroll for Instructor Led Live Python Training


 

0 Responses on Lists concepts in Python"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.