I have a list:
list = [['vegas','London'], ['US','UK']]
How to access each element of this list?
Tried list[:][0] to show all first member for each list inside list is not working. Result will be the same as list[0][:].
So i use list comprehension like this:
print([i[0] for i in list])
which return first element value for each list inside list.
PS: I use variable list as it is the name used in the question, but I would not use this in my own code since it is the basic function list() in Python.
Learn python the hard way ex 34
try this
animals = ['bear' , 'python' , 'peacock', 'kangaroo' , 'whale' , 'platypus']
# print "The first (1st) animal is at 0 and is a bear."
for i in range(len(animals)):
print "The %d animal is at %d and is a %s" % (i+1 ,i, animals[i])
# "The animal at 0 is the 1st animal and is a bear."
for i in range(len(animals)):
print "The animal at %d is the %d and is a %s " % (i, i+1, animals[i])
cities = {"Vegas": "US", "London": "UK"}.list. Take a look here and try not to redefine the default data-structures (list,set,dict,tuple) or anything in the globaldir()that does not belong to you.