Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Lists in python

items = ["bread " ' "fruits" ' "veggies" ]
print("items")
["bread " ' "fruits" ' "veggies " ]
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

lists in python

[0,'',True,5.2,False,[1,2]]
Comment

PREVIOUS NEXT
Code Example
Python :: lol infinite print in python 
Python :: nvidia-smi with user name 
Python :: winwin 
Python :: prettytable in python 
Python :: python as integer ratio 
Python :: token validation in flask socket 
Python :: how to make an app that sends email in python 
Python :: binning continuous values in pyspark 
Python :: python string count complexity 
Python :: child urls python 
Python :: np logical and 
Python :: the most effective search methods in python with example 
Python :: dimension reduction using pca 
Python :: <h1</h1 
Python :: HIDING AND ENCRYPTING USING BASE24 MODULE 
Python :: python sort by last name using lambda 
Python :: Como hacer mayusculas un string 
Python :: negate all elements of a list 
Python :: Basic 13 Algorithm 
Python :: Count occurrence of each term in dataframe except for NaN 
Python :: Closing small holes in the binary image with opencv 
Python :: how to create a sub project in django 
Python :: python arcade sound 
Python :: convert any .pdf file into audio python dev.to 
Python :: how to stop gambling 
Python :: apply with sf 
Python :: how to write a correct python code 
Python :: discord py aliases 
Python :: pandas snippets 
Python :: geopy set proxy 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =