Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
48 views

Python List THEORY

A Python list is a mutable ordered sequence of elements of any type. Lists are defined using square brackets and elements have a fixed integer-indexed position. Lists can contain duplicate elements and elements of different types. Values in a list can be accessed using their integer index or sliced to access sub-sequences. List methods allow adding, removing, and modifying elements. List comprehensions provide a concise way to create lists with expressions and conditionals.

Uploaded by

sonajeon07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
48 views

Python List THEORY

A Python list is a mutable ordered sequence of elements of any type. Lists are defined using square brackets and elements have a fixed integer-indexed position. Lists can contain duplicate elements and elements of different types. Values in a list can be accessed using their integer index or sliced to access sub-sequences. List methods allow adding, removing, and modifying elements. List comprehensions provide a concise way to create lists with expressions and conditionals.

Uploaded by

sonajeon07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python List:

A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements.
Each element or value that is inside of a list is called an item. Just as strings are defined as characters

between quotes, lists are defined by having values between square brackets [ ]

Look at a simple list that stores several names of dogs' breeds:

dog_breeds = ['corgi', 'labrador', 'poodle', 'jack russel']

print(dog_breeds) # ['corgi', 'labrador', 'poodle', 'jack russel']

In the first line, we use square brackets to create a list that contains four elements and then assign it
to the dog_breeds variable. In the second line, the list is printed through the variable's name. All the

elements are printed in the same order as they were stored in the list because lists are ordered.

Here is another list that contains five integers:

numbers = [1, 2, 3, 4, 5]

print(numbers) # [1, 2, 3, 4, 5]

Lists can store duplicate values as many times as needed.

on_off_list = ['on', 'off', 'on', 'off', 'on']

print(on_off_list) # ['on', 'off', 'on', 'off', 'on']

we note that lists are:

ordered, i.e. each element has a fixed position in a list;

iterable, i.e. you can get their elements one by one;

able to store duplicate values;

able to store different types of elements.

Accessing Values in List

1. Indexing : get the single element (Get the item at position)

2. Slicing : get the sublist (subsequence from the sequential quantity) You can return a range of items
by using the

slice syntax.

st = [1, 3, 43, 4] st = [1, 4, 5, 32, 5]

d = st[0] d = st[0:3]

print(d) # output is 1 print(d) # output is [1, 4, 5]


Syntax #create list

# initialize the list (empty list)

L=[]

#integer

print("creating a list with integer number")

num = [10,20,30,40,50]

print("Total list = ",num)

print('first = %d, last = %d' %(num[0] , num[4]))

print()

#create a list with strings

print("creating a list with strings")

names = ["raju", "vani", "venu gopal", "laxmi"]

print('total list= ',names)

print('first = %s, last = %s' %(names[0],names[3]))

print()

#list functions

a = ["red","white","blue"]

b = [3,4,5,6,6]

c=a+b

print(c)

print()

#delete from list

print("delete from index1")

del a[1]

print(a)

print()

#insert in list

a.insert(2,-1) #2=position -1=value

print(a)

print()
#append in list

b.append(333)

print(b)

print()

#index in list

x = a.index("blue") #position of blue

print(x)

print()

#reverse the list

a.reverse()

print(a)

print()

#count

x = b.count(6)

print("it counts the no. of occurence of an element in a list")

print(x)

#create a list with different elements

x = [10,20,20.5,10.5,2.55,"ganesh","vishnu"]

print("total list=",x)

print('first = %d, last = %s' %(x[0],x[6]))

print()

List Comprehensions

List comprehensions provide a concise way to create lists. List comprehension is a complete substitute
for the lambda function as well as the functions map(), filter() and reduce()

The list comprehension starts with a '[' and ']', to help you remember that the

result is going to be a list.

The basic syntax is

[ expression for item in list if conditional ]

This is equivalent to:

for item in list:


if conditional:

expression

L = [2, 43, 5, 453, 5]

Out = [i+10 for i in L] # list comprehension add 10 to each elements

print(Out) # [12, 53, 15, 463, 15 ]

You might also like