list library python code example

Example 1: python list

#Creating lists
my_list = ['foo', 4, 5, 'bar', 0.4]
my_nested_list = ['foobar', ['baz', 'qux'], [0]]

#Accessing list values
my_list[2] # 5
my_list[-1] # 0.4
my_list[:2] # ['foo', 4, 5]
my_nested_list[2] # ['baz', 'quz']
my_nested_list[-1] # [0]
my_nested_list[1][1] # 'qux'

#Modifying lists
my_list.append(x) # append x to end of list
my_list.extend(iterable) # append all elements of iterable to list
my_list.insert(i, x) # insert x at index i
my_list.remove(x) # remove first occurance of x from list
my_list.pop([i]) # pop element at index i (defaults to end of list)
my_list.clear() # delete all elements from the list
my_list.index(x[, start[, end]]) # return index of element x
my_list.count(x) # return number of occurances of x in list
my_list.reverse() # reverse elements of list in-place (no return)
my_list.sort(key=None, reverse=False) # sort list in-place
my_list.copy() # return a shallow copy of the list

Example 2: python list

# example of list in python

myList = [9, 'hello', 2, 'python']

print(myList[0]) # output --> 9
print(myList[-3]) # output --> hello
print(myList[:3]) # output --> [9, 'hello', 2]
print(myList) # output --> [9, 'hello', 2, 'python']

Example 3: python standard library

# https://docs.python.org/3/library/

Example 4: python list

#Creating lists
my_list = [1, "Hello", 3.4, 0, "World"]
my_nested_list = [['Hello', 'World'],[47,39]]

#Accessing lists
my_list[1] # Hello
my_list[-2] # 0
my_list[:3] # [1, "Hello", 3.4]
my_nested_list[1] #[47,39]
my_nested_list[0][1] # World

Example 5: python standard library

>>> timedelta(hours=-5)
datetiSupported operations:

Operation

Result

t1 = t2 + t3

Sum of t2 and t3. Afterwards t1-t2 == t3 and t1-t3 == t2 are true. (1)

t1 = t2 - t3

Difference of t2 and t3. Afterwards t1 == t2 - t3 and t2 == t1 + t3 are true. (1)(6)

t1 = t2 * i or t1 = i * t2

Delta multiplied by an integer. Afterwards t1 // i == t2 is true, provided i != 0.

In general, t1 * i == t1 * (i-1) + t1 is true. (1)

t1 = t2 * f or t1 = f * t2

Delta multiplied by a float. The result is rounded to the nearest multiple of timedelta.resolution using round-half-to-even.

f = t2 / t3

Division (3) of overall duration t2 by interval unit t3. Returns a float object.

t1 = t2 / f or t1 = t2 / i

Delta divided by a float or an int. The result is rounded to the nearest multiple of timedelta.resolution using round-half-to-even.

t1 = t2 // i or t1 = t2 // t3

The floor is computed and the remainder (if any) is thrown away. In the second case, an integer is returned. (3)

t1 = t2 % t3

The remainder is computed as a timedelta object. (3)

q, r = divmod(t1, t2)

Computes the quotient and the remainder: q = t1 // t2 (3) and r = t1 % t2. q is an integer and r is a timedelta object.

+t1

Returns a timedelta object with the same value. (2)

-t1

equivalent to timedelta(-t1.days, -t1.seconds, -t1.microseconds), and to t1* -1. (1)(4)

abs(t)

equivalent to +t when t.days >= 0, and to -t when t.days < 0. (2)

str(t)

Returns a string in the form [D day[s], ][H]H:MM:SS[.UUUUUU], where D is negative for negative t. (5)

repr(t)

Returns a string representation of the timedelta object as a constructor call with canonical attribute values.me.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00

Tags:

Cpp Example