Showing posts with label List In Python. Show all posts
Showing posts with label List In Python. Show all posts

Sunday, January 10, 2021

How To Join Lists In Python

 

In Python there are several techniques to join lists and one of the easiest ways are by using the + operator.

mlist1 = ["a", "b", "c"]

mlist2 = [1, 2, 3]

mlist3 = mlist1 + mlist2

Now mlist3 would be ['a', 'b', 'c', 1, 2, 3]

This '+' operator creates a new list and store the values of the lists. We can join several lists using + operator.


The other technique to join two lists are by appending all the items from one list into another list, one by one. No extra list will be created here. This is carried out using append () method.

Append mlist2 into mlist1:

mlist1 = ["a", "b" , "c"]

mlist2 = [1, 2, 3]

mlist1.append(mlist2)

Now mlist1 would be ['a', 'b', 'c', 1, 2, 3]


extend() method can do the same job as append () method. 

mlist1 = ["a", "b" , "c"]

mlist2 = [1, 2, 3]

mlist1.extend(mlist2)

Now mlist1 would be ['a', 'b', 'c', 1, 2, 3]


Saturday, January 9, 2021

How To Copy List In Python

 To copy an existing list to another in Python, we can use copy () function and the function is invoked on the existing list object. This function returns a new object in which the elements of the existing list is copied.

Copy a List

mylist = ["assam", "banaras", "chennai"]

clist = mylist.copy()


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

Example

mylist = ["assam", "banaras", "chennai"]

clist = list(mylist)


Friday, January 8, 2021

How To Sort List Elements In Python

 List objects in Python have a sort() method that will sort the list alphanumerically, ascending, by default:

Example

Sort the list alphabetically:

mylist = ["Assam", "Asansol", "Banaras", "Delhi", "Odisha"]

mylist.sort()

After sorting the list would be like ["Asansol","Assam", "Banaras", "Delhi", "Odisha"]


Sort the list numerically:

mylist = [100, 50, 65, 82, 23]

mylist.sort()

After sorting the list would be like [23,50,65,82,100]


Sort Descending

To sort descending, use the keyword argument reverse = True in sort () function

Sort the list descending:

mylist = ["Assam", "Asansol", "Banaras", "Delhi", "Odisha"]

mylist.sort(reverse = True)

After sorting the list would be like ["Odisha","Delhi","Banaras", "Assam","Asansol" ]


Sort the list of numeric values in descending order

mylist = [100, 50, 65, 82, 23]

mylist.sort(reverse = True)

After sorting the list would be like [100,82,65,50,23]

Thursday, January 7, 2021

How To Remove Specified List Item In Python


In Python, we can remove elements from list using the value stored in the list or index number

The remove() method removes the specified item.

Example

Remove "Banaras":

mylist = ["Assam","Banaras","Chennai"]

mylist.remove("Banaras")

Now the list will be ["Assam","Chennai"]


Remove Specified Index

The pop() method removes values from list of the specified index.

Example

Remove the second item:

mylist = ["Assam","Banaras","Chennai"]

mylist.pop(1)

Now the list will be ["Assam","Chennai"]


If you do not specify the index, the pop() method removes the last item.

Example

Remove the last item:

mylist = ["Assam","Banaras","Chennai"]

mylist.pop()

The list will be ["Assam","Banaras"]


The del keyword also removes the specified index:

Example

Remove the first item:

mylist = ["Assam","Banaras","Chennai"]

del mylist[0]

Now the list will be ["Banaras","Chennai"]


The del keyword can also delete the list completely.

Example

Delete the entire list:

mylist = ["Assam","Banaras","Chennai"]

del mylist

Clear the List


The clear() method empties the list.

The list still remains, but without any elements.

Example

Clear the list content:

mylist = ["Assam","Banaras","Chennai"]

mylist.clear()


Wednesday, January 6, 2021

How To Change List Items In Python


Like many other programming languages, change value in List is possible in Python.

Change Item Value

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

 Example

Change the second item:

 mylist = ["Assam","Banaras","Chennai"]

mylist[1] = "Bangalore"

Present list will be ["Assam","Bangalore","Chennai"]


 Change a Range of Item Values

To change the value of items within a specific range, we have to specify the start and end index with new values. Changes will be done on consecutive indexes starting from start index to end index -1. 

Example

Change the values "Banaras" and "Chennai" with the values "blackcurrant" and "watermelon":

mylist = ["Assam","Banaras","Chennai", "Delhi","Kolkata","Mumbai"]

mylist[1:3] = ["Bihar", "Kerala"]

 The list will be ["Assam","Bihar","Kerala", "Delhi","Kolkata","Mumbai"]

 

Dynamic insertion is possible in Python. If we want to insert more items than you replace items, the new items will be inserted on consecutive locations and the the remaining items at right side will move accordingly. 

Example

Change the second value by replacing it with two new values: 

mylist = ["Assam","Banaras","Chennai"]

mylist[1:2] = ["Bihar", "Kerala"] (specified location was 1 to 1)

The list will be ["Assam","Bihar","Kerala", "Chennai"] ("Bihar" will be at index 1, "Kerala" at index 2 , "Chennai" at index 3) 

The length of the list will automatically change. 

Like dynamic insertion, dynamic deletion is possible in Python. If we want to insert less items than you replace items, the new items will be inserted on specified indexes.

 

Example

Change the second and third value by replacing it with one value: 

mylist = ["Assam","Banaras","Chennai"]

mylist[1:3] = ["Bihar"]

The list will looks like: ["Assam","Bihar"]

 

Insert Items

To insert a new list item, without replacing any of the existing values, we can use the insert() method. 

The insert() method inserts an item at the specified index:

 

Example

Insert "Bihar" as the third item:

mylist = ["Assam","Banaras","Chennai"]

mylist.insert(2, "Bihar") ( "Bihar" will be inserted at index 2 and "Chennai" will automatically shited to index 3)

The list will be ["Assam","Banaras","Bihar","Chennai"]

Append Items

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

Example
Using the append() method to append an item:

mylist = ["Assam","Banaras","Chennai"]
mylist.append("Mumbai")

Output will be: The list will be ["Assam","Banaras","Bihar","Mumbai"] 

Tuesday, January 5, 2021

How To Access List Items In Python

 

List items are indexed in Python and we can use index number to access them. Index starts from 0 and ends at length of the list -1

 Example

Print the second item of the list:

 mylist = ["Assam", "Banaras", "Chennai"]

print(mylist[1])

 Output: Banaras

 

Negative Indexing in Python is allowed and negative indexing means start from the end

 -1 refers to the last item, -2 refers to the second last item and so on

 Example

Print the last item of the list:

 mylist = ["Assam", "Banaras", "Chennai"]

print(mylist[-1])

 Output: Chennai

 

Range of indexes can be specified to access number of elements at a time. When specifying a range, the return value will be a new list with the specified items.

 Example

Return the third, fourth, and fifth item:

 mylist = ["Assam", "Banaras", "Chennai", "Delhi", "Goa", "Indore"]

print(mylist[startindex:endindex])

The search will start at index 'startindex' and end at 'endindex-1'. If the statement is print(mylist[2:5]), it will fetch values at index 2,3 and 4 and the output would be ['Chennai', 'Delhi', 'Goa']

 

If the 'startindex' is kept vacant, it will become 0

 mylist = ["Assam", "Banaras", "Chennai", "Delhi", "Goa", "Indore"]

print(mylist[:4])

It will fetch values from index 0 to index 3

 

If the 'endindex' is kept vacant, it will fetch values from specified 1st index to last index of the list.

Example

This example returns the items from "cherry" to the end:

 mylist = ["Assam", "Banaras", "Chennai", "Delhi", "Goa", "Indore"]

print(mylist[2:])

 It will display ['Chennai', 'Delhi', 'Goa', 'Indore']


Monday, January 4, 2021

Python Collections – Arrays

  

Collection means storage location of number of values in same named space location.

 

There are four collection data types in the Python programming language:

•           List is a collection which is ordered and changeable. Duplicate elements can be stored in list. 

•           Tuple is a collection which is ordered and unchangeable. It also allows duplicate elements. 

•           Set is a collection which is unordered and unindexed. Duplicate elements not allowed here. 

•           Dictionary is a collection which is unordered and changeable. Duplicate elements not allowed here. 

 

Python Lists

 Lists are used to store multiple items in a single variable. Lists are created using square brackets:

 Example

Create a List:

  mylist = ["Assam", "Benaras", "Chennai"]

print( mylist)

 Output will be: = [‘Assam’, ‘Benaras’, ‘Chennai’]

 

List with duplicate values

Example

Lists allow duplicate values:

  mylist = ["Assam", "Benaras", "Chennai", “Assam”]

print( mylist)

Output will be: = [‘Assam’, ‘Benaras’, ‘Chennai’, ‘Assam’]

 

 List Length

To determine how many items a list has, use the len() function:

 Example

Print the number of items in the list: 

 mylist = ["apple", "banana", "cherry"]

print(len( mylist))

 Output would be: 3

 

List Items - Data Types

 List items can be of any data type:

 Example

String, int and boolean data types:

 list1 = ["Assam", "Benaras", "Chennai"]

list2 = [1, 5, 7, 9, 3]

list3 = [True, False, False]

 

A list can contain different data types:

 Example

A list with strings, integers and boolean values:

 list1 = ["abc", 34, True, 40, "male"]

type()

 From Python's perspective, lists are defined as objects with the data type 'list':

 <class 'list'>

Example

What is the data type of a list?

 

mylist = ["apple", "banana", "cherry"]

print(type(mylist))

 Output would be: <class 'list'>

 

 The list() Constructor

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

 Example

Using the list() constructor to make a List:

 mylist = list(("Assam", "Benaras", "Chennai")) # note the double round-brackets


Subscribe via email

Enter your email address:

Delivered by FeedBurner