Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to use list in python

seller = ['apple', 'banana', 'avocado'] # the list
new_item = 'kiwi' # new item in the store
seller.append(new_item) # now it's have been added to the list
Comment

how to use list in python

list1 = [10, 20, 4, 45, 99]
 
mx=max(list1[0],list1[1])
secondmax=min(list1[0],list1[1])
n =len(list1)
for i in range(2,n):
    if list1[i]>mx:
        secondmax=mx
        mx=list1[i]
    elif list1[i]>secondmax and 
        mx != list1[i]:
        secondmax=list1[i]
 
print("Second highest number is : ",
      str(secondmax))
      
 Output:-     
      
Second highest number is :  45
Comment

list example in python

# list of numbers
n_list = [1, 2, 3, 4]

# 1. adding item at the desired location
# adding element 100 at the fourth location
n_list.insert(3, 100)

# list: [1, 2, 3, 100, 4]
print(n_list)

# 2. adding element at the end of the list
n_list.append(99)

# list: [1, 2, 3, 100, 4, 99]
print(n_list)

# 3. adding several elements at the end of list
# the following statement can also be written like this:
# n_list + [11, 22]
n_list.extend([11, 22])

# list: [1, 2, 3, 100, 4, 99, 11, 22]
print(n_list)
Comment

lists example in python

>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[:]
[1, 4, 9, 16, 25]
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> cubes = [1, 8, 27, 65, 125]  # something's wrong here
>>> 4 ** 3  # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64  # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
>>> cubes.append(216)  # add the cube of 6
>>> cubes.append(7 ** 3)  # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
Comment

python list example

List = [1, 2, 3, "GFG", 2.3]
print(List)
Comment

PREVIOUS NEXT
Code Example
Python :: channel unlock command in discord.py 
Python :: concatenating ols model results 
Python :: menampilkan data dalam range tertentu di python 
Python :: pyglet template 
Python :: separate alphanumeric list 
Python :: pytest handling muliple cases 
Python :: python how to be able to use any python file you made on all projects 
Python :: select randomly from list in loop 
Python :: table is not creating in django 
Python :: horney 
Python :: const in python 3 
Python :: django drf endpoint without model 
Python :: how to replace zero with null in python 
Python :: python project pick text color according to background 
Python :: python ocr pdf dataframe 
Python :: generator expressions python 
Python :: Jhoom.In 
Python :: converting 4hr 20min to minutes 
Python :: pyhton how to chnge colour of graphs 
Python :: dict_leys to list 
Python :: equivalent of case_when in r in pandas 
Python :: fibonacci sequence generator python 
Python :: make my own rabbit bomb using python 
Python :: filter outside queryset in list django 
Python :: palindrome without using string function in python 
Python :: convert string to double 2 decimal places python 
Python :: how to join models from another app 
Python :: order dataframe by specific column c1 
Python :: compute difference in dates after groupby 
Python :: asdict that ignore sentinel 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =