Docsity

Python list explanation programs indexing python repetition iteration of list declaring, Cheat Sheet of Computer Science

Toggle sidebar
bg1
thislist = ["apple",๎˜Ž"banana",๎˜Ž"cherry"]
print(thislist)
Allow Duplicates
thislist = ["apple",๎˜Ž"banana",๎˜Ž"cherry",๎˜Ž"apple",๎˜Ž"cherry"]
print(thislist)
List Length
thislist = ["apple",๎˜Ž"banana",๎˜Ž"cherry"]
print(len(thislist))
1. #๎˜–a๎˜–simple๎˜–list๎˜–๎˜–๎˜–
2. list1๎˜–=๎˜–[1,๎˜–2,๎˜–"Python",๎˜–"Program",๎˜–15.9]๎˜–๎˜–๎˜–๎˜–๎˜–๎˜–
3. list2๎˜–=๎˜–["Amy",๎˜–"Ryan",๎˜–"Henry",๎˜–"Emma"]๎˜–๎˜–๎˜–
4. ๎˜–๎˜–
5. #๎˜–printing๎˜–the๎˜–list๎˜–๎˜–
6. print(list1)๎˜–๎˜–
7. print(list2)๎˜–๎˜–
8. ๎˜–๎˜–
9. #๎˜–printing๎˜–the๎˜–type๎˜–of๎˜–list๎˜–๎˜–
10. print(type(list1))๎˜–๎˜–
11. print(type(list2))๎˜–๎˜–
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Python list explanation programs indexing python repetition iteration of list declaring and more Cheat Sheet Computer Science in PDF only on Docsity!

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

print(thislist)

Allow Duplicates

thislist = ["apple", "banana", "cherry", "apple", "cherry"] print(thislist)

List Length

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

print(len(thislist))

  1. a simple list

  2. list1 = [ 1 , 2 , "Python", "Program", 15.9]
  3. list2 = ["Amy", "Ryan", "Henry", "Emma"]
  4. printing the list

  5. print (list1)
  6. print (list2)
  7. printing the type of list

  8. print (type(list1))
  9. print (type(list2))
  1. list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
  2. print (list[ 0 ])
  3. print (list[ 1 ])
  4. print (list[ 2 ])
  5. print (list[ 3 ])
  6. Slicing the elements

  7. print (list[ 0 : 6 ])
  8. By default, the index value is 0 so its starts from the 0th element and go for index

    -1.
  9. print (list[:])
  10. print (list[ 2 : 5 ])
  11. print (list[ 1 : 6 : 2 ]) Python List Operations The concatenation (+) and repetition (*) operators work in the same way as they were working with the strings. The different operations of list are
  1. Length It is used to get the length of the list Code
  2. size of the list

  3. declaring the list

  4. list1 = [ 12 , 14 , 16 , 18 , 20 , 23 , 27 , 39 , 40 ]
  5. finding length of the list

  6. len(list1) Output: 9
  7. Iteration The for loop is used to iterate over the list elements. Code
  8. iteration of the list

  9. declaring the list

  10. list1 = [ 12 , 14 , 16 , 39 , 40 ]
  11. iterating

  12. for i in list1:
  13. print (i) Output: 12 14 16 39 40
  14. Membership It returns true if a particular item exists in a particular list otherwise false. Code
  15. membership of the list

  16. declaring the list

  1. list1 = [ 100 , 200 , 300 , 400 , 500 ]
  2. true will be printed if value exists

  3. and false if not

  4. print ( 600 in list1)
  5. print ( 700 in list1)
  6. print ( 1040 in list1)
  7. print ( 300 in list1)
  8. print ( 100 in list1)
  9. print ( 500 in list1) Output: False False False True True True Iterating a List A list can be iterated by using a for - in loop. A simple list containing four strings, which can be iterated as follows. Code
  10. iterating a list

  11. list = ["John", "David", "James", "Jonathan"]
  12. for i in list:
  13. The i variable will iterate over the elements of the List and contains each eleme

    nt in each iteration.
  14. print (i) Output: John David James Jonathan Adding Elements to the List
  1. list = [ 0 , 1 , 2 , 3 , 4 ]
  2. print ("printing original list: ");
  3. for i in list:
  4. print (i,end=" ")
  5. list.remove( 2 )
  6. print ("\nprinting the list after the removal of first element...")
  7. for i in list:
  8. print (i,end=" ") Output: printing original list: 0 1 2 3 4 printing the list after the removal of first element... 0 1 3 4 Python List Built-in Functions Python provides the following built-in functions, which can be used with the lists. 1. len() 2. max() 3. min() len( ) It is used to calculate the length of the list. Code
  9. size of the list

  10. declaring the list

  11. list1 = [ 12 , 16 , 18 , 20 , 39 , 40 ]
  12. finding length of the list

  13. len(list1) Output: 6 Max( ) It returns the maximum element of the list

Code

  1. maximum of the list

  2. list1 = [ 103 , 675 , 321 , 782 , 200 ]
  3. large element in the list

  4. print (max(list1)) Output: 782 Min( ) It returns the minimum element of the list Code
  5. minimum of the list

  6. list1 = [ 103 , 675 , 321 , 782 , 200 ]
  7. smallest element in the list

  8. print (min(list1)) Output: 103 Let's have a look at the few list examples. Example: 1- Write the program to remove the duplicate element of the list. Code
  9. list1 = [ 1 , 2 , 2 , 3 , 55 , 98 , 65 , 65 , 13 , 29 ]
  10. Declare an empty list that will store unique values

  11. list2 = []
  12. for i in list1:
  13. if i not in list2:
  14. list2.append(i)
  15. print (list2) Output: [1, 2, 3, 55, 98, 65, 13, 29]

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

print(thislist[ 1 ])

Negative Indexing

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

print(thislist[- 1 ])

Range of Indexes

thislist =

["apple", "banana", "cherry", "orange", "kiwi", "melon", "mang

o"]

print(thislist[ 2 : 5 ])

Check if Item Exists

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

if "apple" in thislist:

print("Yes, 'apple' is in the fruits list")

Change Item Value

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

thislist[ 1 ] = "blackcurrant"

print(thislist)

Change a Range of Item Values

thislist =

["apple", "banana", "cherry", "orange", "kiwi", "mango"]

thislist[ 1 : 3 ] = ["blackcurrant", "watermelon"]

print(thislist)

thislist = ["apple", "banana", "cherry"] thislist[ 1 : 3 ] = ["watermelon"] print(thislist)

Append Items

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

thislist.append("orange")

print(thislist)

Try it Yourself ยป

Insert Items

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

thislist.insert( 1 , "orange")

print(thislist)

Extend List

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

tropical = ["mango", "pineapple", "papaya"]

thislist.extend(tropical)

print(thislist)\

Add Any Iterable

The extend() method does not have to append lists , you can add any

iterable object (tuples, sets, dictionaries etc.).

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

thistuple = ("kiwi", "orange")

thislist.extend(thistuple)

print(thislist)

Remove Specified Item

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

thislist.remove("banana")

print(thislist)

Remove Specified Index

Looping Using List

Comprehension

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

[print(x) for x in thislist]

Python program to demonstrate

Creation of List

Creating a List

List = [] print("Blank List: ") print(List)

Creating a List of numbers

List = [10, 20, 14] print("\nList of numbers: ") print(List)

Creating a List of strings and accessing

using index

List = ["Geeks", "For", "Geeks"] print("\nList Items: ") print(List[0]) print(List[2])