How O Read the List Values Python

Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). 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 fifty-fifty after their creation.

List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is washed with 0 being the first index. Each chemical element in the list has its definite identify in the list, which allows duplicating of elements in the listing, with each element having its own singled-out place and credibility.

Note- Lists are a useful tool for preserving a sequence of data and further iterating over information technology.

Tabular array of content:

  • Creating a List
  • Knowing the size of List
  • Adding Elements to a List:
    • Using append() method
    • Using insert() method
    • Using extend() method
  • Accessing elements from the List
  • Removing Elements from the List:
    • Using remove() method
    • Using popular() method
  • Slicing of a Listing
  • Listing Comprehension
  • Operations on List
  • List Methods

Creating a List

Lists in Python can exist created by simply placing the sequence inside the foursquare brackets[]. Unlike Sets, a list doesn't demand a built-in role for the cosmos of a list.

Note – Unlike Sets, the list may contain mutable elements.

Python3

List = []

print ( "Bare Listing: " )

print ( List )

List = [ ten , 20 , 14 ]

print ( "\nList of numbers: " )

print ( List )

Listing = [ "Geeks" , "For" , "Geeks" ]

print ( "\nList Items: " )

print ( List [ 0 ])

impress ( Listing [ 2 ])

Listing = [[ 'Geeks' , 'For' ], [ 'Geeks' ]]

print ( "\nMulti-Dimensional List: " )

impress ( List )

Output:

Bare List:  []  List of numbers:  [ten, 20, xiv]  List Items Geeks Geeks  Multi-Dimensional List:  [['Geeks', 'For'], ['Geeks']]

Creating a list with multiple distinct or duplicate elements

A list may incorporate indistinguishable values with their distinct positions and hence, multiple distinct or duplicate values can be passed as a sequence at the time of list cosmos.

Python3

List = [ 1 , two , 4 , 4 , iii , iii , 3 , half-dozen , 5 ]

print ( "\nList with the utilize of Numbers: " )

impress ( List )

List = [ ane , 2 , 'Geeks' , 4 , 'For' , 6 , 'Geeks' ]

print ( "\nList with the use of Mixed Values: " )

print ( List )

Output:

List with the use of Numbers:  [1, 2, 4, 4, 3, iii, 3, half dozen, 5]  List with the use of Mixed Values:  [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']

Knowing the size of Listing

Python3

List1 = []

print ( len (List1))

List2 = [ ten , 20 , 14 ]

print ( len (List2))

Adding Elements to a Listing

Using append() method

Elements tin can be added to the List by using the built-in suspend() function. Just one chemical element at a fourth dimension can exist added to the list past using the append() method, for the add-on of multiple elements with the suspend() method, loops are used. Tuples can besides exist added to the list with the employ of the append method because tuples are immutable. Unlike Sets, Lists can also be added to the existing listing with the use of the append() method.

Python3

List = []

impress ( "Initial bare List: " )

impress ( List )

List .append( 1 )

Listing .suspend( two )

List .append( iv )

print ( "\nList later Addition of 3 elements: " )

print ( List )

for i in range ( 1 , 4 ):

List .append(i)

impress ( "\nList later Addition of elements from 1-3: " )

print ( List )

List .suspend(( 5 , 6 ))

impress ( "\nList after Addition of a Tuple: " )

print ( List )

List2 = [ 'For' , 'Geeks' ]

List .append(List2)

print ( "\nList subsequently Addition of a List: " )

print ( List )

Output:

Initial blank Listing:  []  List after Addition of Three elements:  [1, 2, 4]  List afterwards Addition of elements from 1-three:  [i, 2, 4, one, two, three]  List later Addition of a Tuple:  [1, 2, iv, 1, 2, iii, (5, six)]  List after Addition of a List:  [1, two, 4, 1, 2, 3, (5, six), ['For', 'Geeks']]

Using insert() method

append() method only works for the addition of elements at the end of the Listing, for the add-on of elements at the desired position, insert() method is used. Unlike append() which takes only i argument, the insert() method requires two arguments(position, value).

Python3

List = [ one , 2 , 3 , 4 ]

print ( "Initial List: " )

print ( List )

List .insert( 3 , 12 )

List .insert( 0 , 'Geeks' )

print ( "\nList later performing Insert Operation: " )

print ( Listing )

Output:

Initial List:  [1, two, 3, 4]  List after performing Insert Operation:  ['Geeks', ane, 2, 3, 12, 4]

Using extend() method

Other than append() and insert() methods, there'southward one more method for the Add-on of elements, extend(), this method is used to add multiple elements at the same time at the stop of the list.

Annotation – append() and extend() methods can but add elements at the end.

Python3

Listing = [ 1 , 2 , iii , four ]

print ( "Initial List: " )

print ( List )

List .extend([ viii , 'Geeks' , 'Always' ])

impress ( "\nList after performing Extend Operation: " )

print ( List )

Output:

Initial List:  [1, 2, iii, 4]  List after performing Extend Performance:  [i, 2, 3, four, 8, 'Geeks', 'Always']

Accessing elements from the List

In order to access the list items refer to the index number. Utilise the index operator [ ] to access an detail in a listing. The index must exist an integer. Nested lists are accessed using nested indexing.

Python3

List = [ "Geeks" , "For" , "Geeks" ]

print ( "Accessing a element from the list" )

print ( List [ 0 ])

impress ( Listing [ 2 ])

List = [[ 'Geeks' , 'For' ], [ 'Geeks' ]]

impress ( "Accessing a element from a Multi-Dimensional list" )

impress ( List [ 0 ][ 1 ])

print ( List [ 1 ][ 0 ])

Output:

Accessing a element from the list Geeks Geeks Accessing a element from a Multi-Dimensional list For Geeks

Negative indexing

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

Python3

List = [ 1 , 2 , 'Geeks' , 4 , 'For' , 6 , 'Geeks' ]

print ( "Accessing element using negative indexing" )

print ( List [ - i ])

print ( List [ - three ])

Output:

Accessing element using negative indexing Geeks For

Removing Elements from the List

Using remove() method

Elements can be removed from the Listing by using the built-in remove() function but an Fault arises if the element doesn't exist in the list. Remove() method only removes one element at a fourth dimension, to remove a range of elements, the iterator is used. The remove() method removes the specified item.

Note – Remove method in Listing volition only remove the first occurrence of the searched element.

Python3

Listing = [ 1 , 2 , 3 , 4 , 5 , vi ,

7 , 8 , 9 , ten , 11 , 12 ]

print ( "Initial List: " )

print ( Listing )

List .remove( v )

List .remove( 6 )

print ( "\nList after Removal of ii elements: " )

print ( List )

for i in range ( 1 , 5 ):

List .remove(i)

print ( "\nList afterward Removing a range of elements: " )

print ( List )

Output:

Initial List:  [ane, 2, 3, 4, 5, 6, 7, eight, 9, 10, 11, 12]  List after Removal of two elements:  [i, two, 3, 4, 7, 8, 9, x, 11, 12]  List after Removing a range of elements:  [7, viii, 9, x, eleven, 12]

Using pop() method

Popular() office can also be used to remove and return an element from the list, but by default information technology removes only the last element of the list, to remove an element from a specific position of the Listing, the alphabetize of the element is passed as an argument to the pop() method.

Python3

List = [ 1 , 2 , three , 4 , v ]

List .popular()

print ( "\nList after popping an element: " )

impress ( List )

List .pop( 2 )

impress ( "\nList afterwards popping a specific element: " )

impress ( List )

Output:

List after popping an element:  [i, 2, 3, four]  List after popping a specific chemical element:  [1, 2, 4]

Slicing of a Listing

In Python List, there are multiple means to print the whole List with all the elements, but to print a specific range of elements from the list, nosotros utilise the Piece operation. Piece operation is performed on Lists with the use of a colon(:). To print elements from beginning to a range use [: Index], to print elements from end-use [:-Index], to print elements from specific Alphabetize till the finish utilize [Alphabetize:], to print elements within a range, use [Showtime Index:End Index] and to print the whole List with the utilize of slicing functioning, use [:]. Further, to print the whole List in contrary order, use [::-1].

Note – To print elements of List from rear-finish, use Negative Indexes.

python-list-slicing

Python3

Listing = [ 'G' , 'Due east' , 'E' , 'Chiliad' , 'S' , 'F' ,

'O' , 'R' , 'Grand' , 'Eastward' , 'Eastward' , 'K' , 'S' ]

print ( "Initial List: " )

impress ( List )

Sliced_List = List [ three : eight ]

print ( "\nSlicing elements in a range 3-8: " )

print (Sliced_List)

Sliced_List = List [ 5 :]

print ( "\nElements sliced from 5th "

"element till the finish: " )

impress (Sliced_List)

Sliced_List = List [:]

impress ( "\nPrinting all elements using slice performance: " )

print (Sliced_List)

Output:

Initial List:  ['G', 'E', 'Eastward', 'K', 'Southward', 'F', 'O', 'R', 'Grand', 'E', 'E', 'M', 'S']  Slicing elements in a range 3-8:  ['K', 'S', 'F', 'O', 'R']  Elements sliced from 5th chemical element till the end:  ['F', 'O', 'R', 'G', 'Eastward', 'E', 'G', 'S']  Printing all elements using slice performance:  ['G', 'E', 'E', 'K', 'Southward', 'F', 'O', 'R', 'K', 'E', 'E', 'Grand', 'S']

Negative index List slicing

Python3

Listing = [ 'Thou' , 'Eastward' , 'East' , 'Thousand' , 'South' , 'F' ,

'O' , 'R' , 'Thou' , 'E' , 'Eastward' , 'K' , 'S' ]

print ( "Initial List: " )

impress ( Listing )

Sliced_List = List [: - vi ]

print ( "\nElements sliced till 6th element from concluding: " )

impress (Sliced_List)

Sliced_List = List [ - 6 : - i ]

impress ( "\nElements sliced from index -6 to -1" )

print (Sliced_List)

Sliced_List = List [:: - ane ]

print ( "\nPrinting List in reverse: " )

print (Sliced_List)

Output:

Initial Listing:  ['Yard', 'E', 'Due east', 'Yard', 'South', 'F', 'O', 'R', 'G', 'Eastward', 'E', 'K', 'S']  Elements sliced till sixth element from concluding:  ['G', 'E', 'Due east', 'K', 'S', 'F', 'O']  Elements sliced from index -6 to -one ['R', '1000', 'East', 'East', 'K']  Printing List in reverse:  ['Southward', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'One thousand']

List Comprehension

Listing comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc.
A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.

Syntax:

newList = [ expression(element) for element in oldList if condition ]

Case:

Python3

odd_square = [x * * two for ten in range ( one , 11 ) if x % 2 = = 1 ]

print (odd_square)

Output:

[1, ix, 25, 49, 81]

For amend understanding, the higher up code is similar to –

Python3

odd_square = []

for x in range ( 1 , eleven ):

if 10 % two = = i :

odd_square.append(10 * * ii )

print (odd_square)

Output:

[1, 9, 25, 49, 81]

Refer to the beneath articles to become detailed data about List Comprehension.

  • Python List Comprehension and Slicing
  • Nested Listing Comprehensions in Python
  • Listing comprehension and ord() in Python

Operations on List

  • Find length of list
  • Iterate over a listing in Python
  • Concatenating two lists in Python
  • List Membership Exam

Listing Methods

Office Description
Append() Add an element to the end of the list
Extend() Add all elements of a list to another list
Insert() Insert an detail at the defined index
Remove() Removes an item from the list
Popular() Removes and returns an chemical element at the given alphabetize
Clear() Removes all items from the listing
Index() Returns the alphabetize of the showtime matched item
Count() Returns the count of the number of items passed equally an argument
Sort() Sort items in a list in ascending order
Reverse() Reverse the order of items in the list
re-create() Returns a copy of the listing

Born functions with Listing

Part Description
reduce() apply a item office passed in its argument to all of the list elements stores the intermediate result and but returns the final summation value
sum() Sums up the numbers in the list
ord() Returns an integer representing the Unicode code point of the given Unicode character
cmp() This function returns 1 if the first listing is "greater" than the second list
max() return maximum element of a given listing
min() return minimum element of a given list
all() Returns true if all element is truthful or if the list is empty
any() return true if whatsoever chemical element of the list is truthful. if the list is empty, return simulated
len() Returns length of the list or size of the list
enumerate() Returns enumerate object of the list
accumulate() apply a item role passed in its argument to all of the list elements returns a list containing the intermediate results
filter() tests if each element of a list is true or non
map() returns a list of the results afterwards applying the given function to each item of a given iterable
lambda() This role can have whatever number of arguments but merely one expression, which is evaluated and returned.

Recent articles on Lists

More videos on Python Lists: Python List – Set 2

More on Python Listing –

  • Creating a 3D Listing
  • Iterate over a list in Python
  • Iterate over multiple lists simultaneously
  • Internal working of listing in Python
  • Python Slicing
  • Python List Comprehensions vs Generator Expressions
  • List Methods in Python – Fix 1 Set up two
  • Lambda expression and filter part

Useful Links:

  • Recent Articles on Python List
  • Python Tutorials
  • Python Output Programs in List: Gear up 6, Fix 11, Set 12, Fix 13
  • Multiple Option Questions
  • All articles in Python Category

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To brainstorm with, your interview preparations Enhance your Data Structures concepts with the Python DS Class. And to begin with your Automobile Learning Journey, join the Car Learning - Basic Level Course


gabelimensid.blogspot.com

Source: https://www.geeksforgeeks.org/python-list/

0 Response to "How O Read the List Values Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel