Python List With Example

We have seen what is frozenset and set in python. Now, we will learn about python list with example. We will see how to create python list, add item to list, modify items or delete items in python list.

Getting Started

Lists are one of 4 built-in data structure used to store items of the same or different data type. In opposition to set items, lists items are ordered and mutable. In short, lists items can be modified, retrieved by its items and each item can be present more than one time.

name = [ "John", "Joe", "Jason", "Junior", "John" , "Jack" ]

Here, name is a list in which elements are John, Joe, Jason, Junior, John and Jack.

Notice that John is occurring more than one time in list.

Let’s see another example,

notes = [ 1, 2, 3, 4, 5, 2, 6, 8, 9, 9, 10 ]

Here, notes is another list in which elements are 1, 2, 3, 4, 5, 2, 6, 8, 9, 9 and 10.

Let’s see another example,

items = [ "John", 1, "Jason", 6, "John", 8, "Mike", 10 ]

Here,
items is another list in which elements are John, 1, Jason, 6, John, 8, Mike and 10.

Notice that items list contains elements of different data type.

Create List in Python

List in python can be created by several means –

Tutorialwing Python List With Example of Create List in Python

  1. Using list() Constructor

    List in python can be created using list() constructor as shown below –

    word = list("Python")
    print(word)
    

    Output:

    ['P', 'y', 't', 'h', 'o', 'n']
    
  2. Using Square Bracket i.e. [ ]

    [ ] with some elements within it creates list with those elements as shown below –

    course = [ "Chemistry", "History", "Mathematic", "Physic", "Biology" ]
    print(course)
    

    Here,
    Chemistry, History, Mathematic, Physic and Biology are elements of list in python.

    Output:

    ['Chemistry', 'History', 'Mathematic', 'Physic', 'Biology']
    
  3. Using List Comprehension

    List comprehension can also be used to list in python as shown below –

    numbers = [ number for number in range(15) ] print(numbers)
    

    Output:

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
    

    We will discuss about list comprehension more in this post later.

Create Empty List in Python

To create an empty list, we use below syntax –

newList = [ ]

Here, a new list is created using [ ] and stored in variable newList.

Getting Length or Size of Python List

The Python function len() is used to get the number of elements present in any sequence object. Thus, when we want to get the number of elements present in a list or get list size, we use the len() function which takes a list as a parameter and returns the number of elements in list.

For example,

course = [ "Chemistry", "History", "Mathematic", "Physic", "Biology" ]
print(len(course))

Output:

5

Here, we have only 5 elements in list course.

For displaying elements present in list in Python, we have the choice to

  1. Use loops method or,
  2. Use unpacking method

These two methods are followed by the Python internal function print().

  1. We can print elements of python List using for and while loop –

    • Using For Loop

      We can use for loop and print() method to print elements of list in python as shown below –

      course = ["Chemistry", "History", "Mathematic", "Physic", "Biology" ]
      for item in course :
          print(item, end= " ")
      

      Output:

      Chemistry History Mathematic Physic Biology 
      
    • Using while Loop

      We can use while loop and print() method to print elements of list in python as shown below –

      country = [ "India", "China", "Japan", "Russia", "Belgica" ] 
      item = 0
      while item < len(country) :
          print(country[item], end= " ") 
          item += 1
      

      Output:

      India China Japan Russia Belgica
      
  2. We can use unpacking method as shown below to print elements of python list –

    fruits = [ "Orange", "Mango", "Banana" ]
    print(*fruits)
    

    Output:

    Orange Mango Banana
    

Visit our exclusive post on 7 Different ways to print Elements of List in Python.

Access Elements of List in Python

When we want to access or iterate list items in python, we can use various ways –

Tutorialwing Python List Indexing and Negative Indexing Example of Python List

Above image depicts a list in which elements are A, B, C, D, E, F, G and H. Checkout the index and negative index in it.

  • Using List Index

    To access an item in a list in Python, we can use square brackets [ ] called the index operator. In Python and other languages, indices start at 0. Thus, the indice of a list that contains 15 items will start from 0 to 14. In the same way, a list that contains 3 items will have indices which start from 0 to 2.

    For example,

    languages = [ "PHP", "Python", "Java", "Java Script", "Kotlin" ]
    print(languages[1])
    

    Output:

    Python
    

    Note : List indices must be integers not float values otherwise we will get a TypeError.

    For example,

    languages = [ "PHP", "Python", "Java", "Java Script", "Kotlin" ]
    print(languages[1.0])
    

    Output:

    TypeError: list indices must be integers or slices, not float
    

    Note : When the list indice doesn’t have corresponding value in a list, this will result in IndexError.

    For example,

    languages = [ "PHP", "Python", "Java", "Java Script", "Kotlin" ]
    print(languages[7])
    

    Output:

    IndexError: list index out of range
    
  • Using Negative Index

    In python, apart from above method negative index can be used to access sequences objects. Thus, the index of -1 corresponding to the last element in the list, -2 corresponding to the second last element and so on.

    For example,

    word = list("Tutorialwing")
    print(word[-1])
    

    Output:

    g
    

    Let’s see another example,

    word = list("Tutorialwing")
    print(word[-4])
    

    Ouptut:

    w
    

List Slicing in Python

Regardless the ways mentioned previously to access list items, list slicing is also used to access a range of list items in Python.

Here, we will get all item of list –

languages = [ "PHP", "Python", "Java", "Java Script", "Kotlin" ]
print(languages[:])

Output:

['PHP', 'Python', 'Java', 'Java Script', 'Kotlin']

Let’s see another example,

languages = [ "PHP", "Python", "Java", "Java Script", "Kotlin" ]
print(languages[0:])

Output:

['PHP', 'Python', 'Java', 'Java Script', 'Kotlin']

Get elements from index 2 to 4

languages = [ "PHP", "Python", "Java", "Java Script", "Kotlin" ]
print(languages[2:5])

Output:

['Java', 'Java Script', 'Kotlin']

Get elements from index 0 to 2

languages = [ "PHP", "Python", "Java", "Java Script", "Kotlin" ]
print(languages[:3])

Output:

['PHP', 'Python', 'Java']

We can also list slicing by specify step.

languages = [ "PHP", "Python", "Java", "Java Script", "Kotlin" ]
print(languages[0 : : 2])

Output:

['PHP', 'Java', 'Kotlin']

Note : When we use slicing, the start index is included contrary to the end index which is excluded.

Add Element in Python List

When a list is created, we can add another item to it. To add item to list, we can use below ways –

  • Use append() Method

    languages = [ "PHP", "Python", "Java", "Java Script", "Kotlin" ] 
    languages.append("Dart")
    print(languages)
    

    Output:

    ['PHP', 'Python', 'Java', 'Java Script', 'Kotlin', 'Dart']
    

    Let’s see another example,

    name = [ "John", "Doe", "Martin", "Jack" ] 
    name.append(["Mike", "Junior"]) 
    print(name)
    

    Output:

    ['John', 'Doe', 'Martin', 'Jack', ['Mike', 'Junior']]
    

    Note : append() method add an element or an iterable at the end of list and return None. Iterable can be a list, a tuple, a dictionary or string sequence.

  • Use extend() Method

    languages = [ "HTML", "CSS", "PHP", "Java Script"] 
    languages.extend(["Python", "Java", "Dart", "Kotlin" ]) 
    print(languages)
    

    Output:

    ['HTML', 'CSS', 'PHP', 'Java Script', 'Python', 'Java', 'Dart', 'Kotlin']
    

    Note : extend() method takes only an iterable as parameter and add its items at the end of list.

  • Use + Operator

    languages = [ "HTML", "CSS", "PHP", "Java Script"] + ["Python", "Java" ] 
    print(languages)
    

    Output:

    ['HTML', 'CSS', 'PHP', 'Java Script', 'Python', 'Java']
    

    Note: + operator adds two list and returns resultant list.

Update Element of List in Python

In Python, after list is created we have the possibility to modify or update its items by using index of item or position of item.

For example,

languages = [ "HTML", "CSS", "PHP", "Java Script"] 
languages[1] = "Rust"
print(languages)

Output:

['HTML', 'Rust', 'PHP', 'Java Script']

Let’s take another example,

languages = [ "HTML", "CSS", "PHP", "Java Script"] 
languages[-1] = ["Type Script", "Cobol" ] 
print(languages)

Output:

['HTML', 'CSS', 'PHP', ['Type Script', 'Cobol']]

Note : We can update item present in list by using any type of object. Then item can be updated by using List, Tuple, Dictionary, Integers, Float, String and so on.

For example,

framework = [ "Django", "Laravel"] 
framework[1] = ("Flutter", "Xamarin") 
print(framework)

Output:

['Django', ('Flutter', 'Xamarin')]

Let’s take another example,

framework = ['Django', ('Flutter', 'Xamarin')] 
framework[0] = {"Python" : "Kivy"} 
print(framework)

Output:

[{'Python': 'Kivy'}, ('Flutter', 'Xamarin')]

Let’s take another example,

framework = [{'Python': 'Kivy'}, ('Flutter', 'Xamarin')] 
framework[0] = {"Bootstrap" , "Spring Boot", "Thymeleaf"} 
print(framework)

Output:

[{'Bootstrap', 'Thymeleaf', 'Spring Boot'}, ('Flutter', 'Xamarin')]

Let’s take another example,

newList = [ 10, 14, 18, 20, 40, 50, 60, 80 ] 
newList[4] = 1000
print(newList)

Output:

[10, 14, 18, 20, 1000, 50, 60, 80]

Let’s take another example,

newList = [ 10, 14, 18, 20, 40, 50, 60, 80 ] 
newList[2] = 1.5
print(newList)

Output:

[10, 14, 1.5, 20, 40, 50, 60, 80]

Delete Elements in Python List

List element can be deleted by using below methods –

  • Using remove() Method

    remove() method removes first occurrence of given element from the list.

    For example,

    name = [ "John", "Doe", "Martin", "Jack" ] 
    name.remove("Jack")
    print(name)
    

    Output:

    ['John', 'Doe', 'Martin']
    

    Note : remove() method raises ValueError if item is not present in list.

  • Using pop() Method

    pop() method removes item at given position and return it. By default the last item is removed if position is not specified.

    For example,

    name = [ "John", "Doe", "Martin", "Jack" ] 
    name.pop(1)
    print(name)
    

    Output:

    ['John', 'Martin', 'Jack']
    

    Let’s take another example,

    name = [ "John", "Doe", "Martin", "Jack"] 
    name.pop()
    print(name)
    

    Output:

    ['John', 'Doe', 'Martin']
    

    Note: pop() method raises an IndexError if item corresponding to given position isn’t the list.

  • Using clear() Method

    clear() method removes all items present in list.

    For example,

    name = [ "John", "Doe", "Martin", "Jack" ] 
    name.clear()
    print(name)
    

    Output:

    [ ]
    
  • Using del Keyword

    del keyword allows to remove list completely.

    For example,

    name = [ "John", "Doe", "Martin", "Jack" ] 
    del name
    print(name)
    

    Output:

    NameError: name 'name' is not defined
    

    del keyword is different to clear() method. The difference between them is that del keyword delete list completely whereas clear() method remove all list items and keep list empty.

Till now, we have seen different operations using python list with example. Now, we will see different methods in list –

Python List Methods

In this section, we are going to show you methods present in Python List.
In python, several list methods are available and they help us to work properly with list in python.

Method Description
append()
  • Adds only one item to the end of list.
  • Item can be iterable or number
extend()
  • Update list by adding all items in one list to the end of another list.
  • extend() method takes only iterable as parameter.
clear() Removes all items present in the list.
index()
  • Takes list item as parameter and returns the first index corresponding to this parameter.
  • index() method raises ValueError if item is not present in list.
pop()
  • Takes as parameter index of list item, removes and returns item corresponding to the index.
  • pop() method raises an IndexError if index is not present in a list or list is empty.
  • By default when index is not specified, pop method removes the last items present in a list.
insert() Takes index and item as parameters. Then, inserts that item at the given index.
remove()
  • Takes item as parameter and remove the first occurrence of this item.
  • remove() method raises ValueError if item is not present in list.
count() Returns the number of occurrences of item taken as parameter.
sort() Sorts the list in ascending order and return None.
reverse() Reverses the list in descending order and return None.
copy() Returns a shadow copy of the list.

Python List Comprehension

List comprehensions are concise way or convenient notation for creating new lists in Python. They consist of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

List comprehension with for clause

For example,

list1 = [item for item in range(1, 6)]
print(list1)

Output:

[1, 2, 3, 4, 5]

Let’s take another example,

list2 = [item ** 3 for item in range(1, 6)] 
print(list2)

Output:

[1, 8, 27, 64, 125]

Let’s take another example,

colors = ['red', 'orange', 'yellow', 'green', 'blue'] 
colors1 = [item.upper() for item in colors] 
print(colors1)

Output:

['RED', 'ORANGE', 'YELLOW', 'GREEN', 'BLUE']

List comprehension with if clause

For example,

list3 = [item for item in range(1, 11) if item % 2 == 0]
print(list3)

Output:

[2, 4, 6, 8, 10]

Let’s take another example,

list4 = [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] 
print(list4)

Output:

[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

Let’s take another example,

matrix = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]] 
new_list = [[row[i] for row in matrix] for i in range(4)] 
print(new_list)

Output:

new_list = [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

Now, we will see different operations in python list with example –

Different List Operations in Python

Now, we will go through different python list operations –

  • Check if element exist or not in list.

    To verify if item is present or not in list we use this syntax :

    item in list name
    

    It returns true if item is present in list. Otherwise, returns false.

    OR,

    item not in list name
    

    It returns true if item is not present in the list. Otherwise, returns false.

    The result of above statement is boolean.
    When the operation is verified, we get True. Otherwise False if not.

    For example,

    list5 = [1, 8, 27, 64, 125] 
    result = 8 in list5
    print(result)
    

    Output:

    True
    

    Let’s take another example,

    list5 = [1, 8, 27, 64, 125] 
    result = 8 not in list5
    print(result)
    

    Output:

    False
    

    Let’s take another example,

    list5 = [1, 8, 27, 64, 125] 
    result = 10 not in list5
    print(result)
    

    Output:

    True
    

    Let’s take another example,

    list5 = [1, 8, 27, 64, 125] 
    result = 10 in list5
    print(result)
    

    Output:

    False
    
  • Iterating through List in Python

    To make an iteration through a list, for statement or while statement is used.

    1. Iterate Using for Statement

      For example,

      words = ['cat', 'window', 'defenestrate'] 
      for word in words :
          print(word, len(word))
      

      Output:

      cat 3
      window 6
      defenestrate 12
      

      Here, we are iterating list words and printing each item and it’s length.
      len(word) returns length of word.

      Let’s take another example,

      words = ['cat', 'window', 'defenestrate']
      for position, word in enumerate(words) :
          print(position, word)
      

      Output:

      0 cat
      1 window
      2 defenestrate
      

      Let’s take another example,

      number = [] 
      for i in range(20) :
          number.append(i)
      
      print(number)
      

      Output:

      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
      
    2. Iterate Using while Statement

      For example,

      words = ['cat', 'window', 'defenestrate']
      i = 0
      while i < len(words) :
          print(words[i])
          i+=1
      

      Output:

      cat
      window
      defenestrate
      
  • Copy One List to Other List in Python

    To copy a list to another list we can do so

    Let’s go through each one by one –

    • Using copy() Method

      We can copy one list into another using copy() method as shown below –

      words1 = ['cat', 'window', 'defenestrate'] 
      words2 = words1.copy()
      print(words2)
      

      Output:

      ['cat', 'window', 'defenestrate']
      
    • Using = Operator

      We can use = operator in python to copy one list into another as shown below –

      words1 = ['cat', 'window', 'defenestrate'] 
      words2 = words1
      print(words2)
      

      Output:

      ['cat', 'window', 'defenestrate']
      
  • Sorting Python List

    In Python, to arrange items of a list in ascending order we can

    Let’s go through each one by one –

    • Use list method sort()

      sort() method sorts the given list in ascending order.

      For example,

      words1 = ['cat', 'window', 'defenestrate'] 
      words1.sort()
      print(words1)
      

      Here, we are sorting list word1.
      Output:

      ['cat', 'defenestrate', 'window']
      

      Let’s take another example,

      name = ['John', 'Doe', 'Martin', 'Jack'] 
      name.sort()
      print(name)
      

      Output:

      ['Doe', 'Jack', 'John', 'Martin']
      

      Let’s take another example,

      number = [10, 3, 5, 2, 4, 9, 6] 
      number.sort()
      print(number)
      

      Output:

      [2, 3, 4, 5, 6, 9, 10]
      
    • Python internal function sorted

      sorted function takes list as argument and sorts it in ascending order.

      For example,

      name = ['John', 'Doe', 'Martin', 'Jack'] 
      name = sorted(name)
      print(name)
      

      Output:

      ['Doe', 'Jack', 'John', 'Martin']
      
  • Reversing List in Python

    For reversing list or arrange list in reverse order in Python, we can do it –

    Let’s check one by one –

    • Using list Method – reverse()

      reverse() method sorts given list in reverse order.

      For example,

      number = [2, 3, 4, 5, 6, 9, 10] 
      number.reverse()
      print(number)
      

      Output:

      [10, 9, 6, 5, 4, 3, 2]
      

      Let’s take another example,

      name = ['John', 'Doe', 'Martin', 'Jack'] 
      name.reverse()
      print(name)
      

      Output:

      ['Jack', 'Martin', 'Doe', 'John']
      
    • Python internal function – reversed()

      Reversing integers is different from reversing string sequence because about string sequence, reverse() method compare each character present in string sequence.

      For example,

      number = [2, 3, 4, 5, 6, 9, 10]
      reveredNum = reversed(number)
      for item in reveredNum:
          print(item, end=' ')
      

      Output:

      10 9 6 5 4 3 2
      
  • Duplicate List Item in Python

    In Python to duplicate items contain in a list, we use the mathematical operator * followed by a number that represent how many time items will be duplicated.

    Let’s take another example,

    number = [2, 3, 4, 5, 6, 9, 10] * 2
    print(number)
    

    Output:

    [2, 3, 4, 5, 6, 9, 10, 2, 3, 4, 5, 6, 9, 10]
    

Nested List in Python

Nested lists in Python are lists that contain another list.

For example,

languages = [ ["HTML", "CSS", "PHP"], ["Python", "Java", "Dart", "Kotlin" ] ]
print(languages)

Output:

[['HTML', 'CSS', 'PHP'], ['Python', 'Java', 'Dart', 'Kotlin']]

Access Items in Nested List in Python

After creating nested list we can access its elements by using :

  • Using Indexing

    Like indexing in list, items in nested list can also be accessed using indexing.

    For example,

    languages = [ ["HTML", "CSS", "PHP"], ["Python", "Java", "Dart", "Kotlin" ] ] 
    print(languages[1])
    

    Output:

    ['Python', 'Java', 'Dart', 'Kotlin']
    

    Let’s see another example,

    languages = [ ["HTML", "CSS", "PHP"], ["Python", "Java", "Dart", "Kotlin" ] ]
    print(languages[0][2])
    

    Output:

    PHP
    

    Note : In this Python code we have a list that contains another lists. Then the first list in the nested list correspond to indice 0 and so on. So to access an item in nested list we must specify first of all the list index and the item index.

    Let’s see another example,

    languages = [ ["HTML", "CSS", "PHP"], ["Python", "Java", "Dart", "Kotlin" ] ] 
    print(languages[1][1])
    

    Output:

    Java
    
  • Using Negative Index

    Like negative indexing in list, items in nested list can also be accessed as shown below –

    For example,

    languages = [ ["HTML", "CSS", "PHP"], ["Python", "Java", "Dart", "Kotlin" ] ]
    print(languages[1][-1])
    

    Output:

    Kotlin
    

    Let’s take another example,

    languages = [ ["HTML", "CSS", "PHP"], ["Python", "Java", "Dart", "Kotlin" ] ] 
    print(languages[0][-2])
    

    Output:

    CSS
    
  • List Slicing in Nested List

    Like list slicing, we can also apply same concept in nested list –

    For example,

    languages = [ ["HTML", "CSS", "PHP"], ["Python", "Java", "Dart", "Kotlin" ] ]
    print(languages[0][0 : 3])
    

    Output:

    ["HTML", "CSS", "PHP"]
    

    Let’s see another example,

    languages = [ ["HTML", "CSS", "PHP"], ["Python", "Java", "Dart", "Kotlin" ] ]
    print(languages[1][1 : 3])
    

    Output:

    ["Java", "Dart"]
    

    Note : We can apply the ways we use to access lists items to nested lists. Here, an item involves to a list so we must access a list before applying ways.

Other Functions Working with Lists

Function Description
sum() Returns the sum of items if it contains an iterable of integers. Returns 0 when the iterable is empty.
max() With a single iterable argument, returns its biggest item. With two or more arguments, returns the largest argument.
min() With a single iterable argument, returns its smallest item. With two or more arguments, returns the smallest argument.
enumerate() Takes an iterable of integers as argument and returns index or position of item and item corresponding to each position respectively.
len() Returns the number of items in a container, list in our case.
type() Returns the type of argument, variable and so on. In our case variable type will be a list.

Mathematic Operator in Lists

Operator Description
Operator of addition (+) Concatenate two or more lists and returns another list that contain all items presents in old lists.
Operator of multiplication (*) Duplicates items in a list.
Operator of equality (=) Copy all items presents in a list to another list.

Now, we will see different multiple choice questions in python list with example –

Multiple Choice Questions in Python List

1. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse"]
print(components[-1])

a.) Screen
b.) None
c.) Mouse
d.) Compile Error

2. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse"] 
components.append("Charger") 
print(components)

a.) [“Screen”, ” Charger “, “Keyboard”, “Mouse”]
b.) [“Screen”, “Keyboard”, “Mouse”, ” Charger “]
c.) None
d.) Compile Error

3. Which of the below statement(s) is/are wrong ?

a. List is mutable
b. List doesn’t allow duplicated items
c. List items are unordered.

4. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse"] * 3
print(components)

a.) [“Screen”, “Keyboard”, “Mouse”, “Screen”, “Keyboard”, “Mouse]
b.) [“Screen”, “Keyboard”, “Mouse”, “Screen”, “Keyboard”, “Mouse”, “Screen”, “Keyboard”, “Mouse”]
c.) [“Screen”, “Keyboard”, “Keyboard”, “Mouse”, “Screen”, “Keyboard”, “Mouse”, “Keyboard”, “Mouse”]
d.) [“Screen”, “Keyboard”, “Keyboard”, “Mouse”, “Keyboard”, “Mouse”]

5. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse", "Charger"]
print(components[0 : 4 ])

a.) [“Screen”, “Keyboard”, “Mouse”, “Charger”]
b.) [“Screen”, “Keyboard”, “Mouse”]
c.) [“Screen”, “Mouse”]
d.) [“Keyboard”, “Mouse”]

6. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse", "Charger"] 
result = "Keyboard" not in components
print(result)

a.) False
b.) True
c.) None
d.) Type Error

7. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse", "Charger"] 
components.sort()
print(components)

a.) [“Screen”, “Keyboard”, “Mouse”, “Charger”]
b.) [“Charger”, “Mouse”, “Keyboard”, “Screen”]
c.) [“Charger”, “Keyboard”, “Mouse”, “Screen”]
d.) None

8. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse", "Charger"] 
for i in range(3) :
    print(components[i], end=" ")

a.) Screen Keyboard Mouse
b.) Screen Mouse Keyboard
c.) Mouse Screen Keyboard
d.) None

9. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse", "Charger"] 
components.remove("Key")
print(components)

a.) [“Screen”, “Keyboard”, “Mouse”, “Charger”]
b.) [“Screen”, “Keyboard”, “Mouse”, “Charger”, “Key”]
c.) ValueError
d.) None

10. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse", "Charger"]
print(max(components))

a.) Screen
b.) Keyboard
c.) Mouse
d.) Charger

11. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse", "Charger"]
print(min(components))

a.) Screen
b.) Keyboard
c.) Mouse
d.) Charger

12. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse", "Charger"] 
components.pop(4)
print(components)

a.) [“Screen”, “Keyboard”, “Mouse”]
b.) [“Keyboard”, “Mouse”, “Charger”]
c.) [“Screen”, “Keyboard”, “Charger”]
d.) IndexError

13. Guess the output of the program

components = ["Screen", "Keyboard", "Mouse", "Charger"]
print(components.index("Charger"))

a.) 1
b.) 3
c.) 4
d.) 2

14. Choose the correct syntax for creating empty list.

a.) list
b.) [ ] 
c.) list()
d.) None of Above

15. Guess the output of the program

number = [[10, 3, 74, 1, 9], [ 73, 13, 91], [12, 32, 72, 12, 99], [11, 33, 77, 11, 90]]
print(number[2][1:])

a.) [13, 91]
b.) [32, 72, 12, 99]
c.) [33, 77, 11, 90]
d.) [3, 74, 1, 9]

That’s end of our post on python list with example. visit official website to lean more.

Leave a Reply