Python list of indexes - python

I would like to get the indexes of a given list (images) and put it into a list.
So far I've tried:
list(images)
But this only returns itself.
list(images.keys())
But this gives me an error that images has no attribute keys
Is there a way to get all the indexes into its own list?

You could simply create a range as long as the list:
range(len(images))
This returns Range object, which is usable in most of the same contexts as a list. If you really wanted it as a list however, you could just put the range in a list:
list(range(len(images)))

Related

Python Search Dictionary by Key to return Value

I have a fairly complex dictionary with lists that I'm trying to use. I'm attempting to search the dictionary for the key "rows", which has a list.
With my current sample of data, I can easily pull it like so with index operator:
my_rows = my_dict['inputContent']['document']['fields'][2]['value']['rows']
'rows' is a list and those are the values I am trying to pull. However, my data from 'rows' won't always be in the exact same location but it will always be in my_dict. It could be in a different index like so:
my_dict['inputContent']['document']['fields'][4]['value']['rows']
or
my_dict['inputContent']['document']['fields'][7]['value']['rows']
Really any number.
I've tried using just the basic:
my_rows = my_dict.get("rows")
But this returns None.
I find lots of articles on how to search for values and return key, but I know the key and it will always be the same, while my values in 'rows' will always be different.
I'm new to python and using dictionaries in general, but i'm really struggling to drill down into this dictionary to pull this list.
my_dict['inputContent']['document']['fields'][2]['value']['rows']
my_dict['inputContent']['document']['fields'][4]['value']['rows']
my_dict['inputContent']['document']['fields'][7]['value']['rows']
Looks like the overall structure is the same, the only variable is the numeric list index of the fourth element. So we need a loop that iterates over each element of that list:
for element in my_dict['inputContent']['document']['fields']:
if 'rows' in element['value']:
# found it!
print(element['value']['rows'])

How do you rename a list to a variable in Python

I'm trying to create a random string of numbers saved to a list, then rename the list further in the code. I've tried using dictionaries to store the names but that didn't work, I've also tried simply newListName=oldListName which when I attempted to print newListName it returns a NameError
You can copy the list into a new one as:
newListName=oldListName[:]
This would not give you the NameError!
Make sure that oldListName is defined as list before and then copied into another list.

List append in a "for" loop

I am trying to get a list of elements using a for loop to index my previous list. Here's the code:
for index in range(1,810):
list.extend(t[index])
list.extend(t[index+1])
list.extend(t[index])
I already have a list named "list" and t is an other list.
To be more specific I want to make my list like this.
t1,t2,t1,t2,t3,t2,t3,t4 etc.
I get this error TypeError: 'float' object is not iterable.
You should be using list.append since list.extend works on iterables; i.e. you can do stuff like lst=[1,2,3];lst.extend([4,5]) which would give you [1,2,3,4,5]
see this link if you want to read more
The other answers suggesting you use append fix your issue, but appending is very inefficient. You should use a list comprehension. Also, "list" is a keyword, so you should use another name.
my_list = [item for index in range(1,810) for item in [t[index],t[index+1],t[index]] ]

Create new list using other list to look up values in dictionary - python

Consider the below situation. I have a list:
feature_dict = vectorizer.get_feature_names()
Which just have some strings, all of which are a kind of internal identifiers, completely meaningless. I also have a dictionary (it is filled in different part of code):
phoneDict = dict()
This dictionary has mentioned identifiers as keys, and values assigned to them are, well, good values which mean something.
I want to create a new list preserving the order of original list (this is crucial) but replacing each element with the value from dictionary. So I thought about creating new list by applying a function to each element of list but with no luck.
I tried to create a fuction:
def fastMap(x):
return phoneDict[x]
And then map it:
map(fastMap, feature_dict)
It just returns me
map object at 0x0000000017DFBD30.
Nothing else
Anyone tried to solve similar problem?
Just convert the result to list:
list(map(fastMap, feature_dict))
Why? map() returns an iterator, see https://docs.python.org/3/library/functions.html#map:
map(function, iterable, ...)
Return an iterator that applies function
to every item of iterable, yielding the results. If additional
iterable arguments are passed, function must take that many arguments
and is applied to the items from all iterables in parallel. With
multiple iterables, the iterator stops when the shortest iterable is
exhausted. For cases where the function inputs are already arranged
into argument tuples, see itertools.starmap().
which you can convert to a list with list()
Note: in python 2, map() returns a list, but this was changed in python 3 to return an iterator

Python inserting lists into a list with given length of the list

My problem is, that need a list with length of 6:
list=[[],[],[],[],[],[]]
Ok, that's not difficult. Next I'm going to insert integers into the list:
list=[[60],[47],[0],[47],[],[]]
Here comes the real problem: How can I now extend the lists and fill them again and so on, so that it looks something like that:
list=[[60,47,13],[47,13,8],[1,3,1],[13,8,5],[],[]]
I can't find a solution, because at the beginning i do not know the length of each list, I know, they are all the same, but I'm not able to say what length exactly they will have at the end, so I'm forced to add an element to each of these lists, but for some reason i can't.
Btw: This is not a homework, it's part of a private project :)
You don't. You use normal list operations to add elements.
L[0].append(47)
Don't use the name list for your variable it conflicts with the built-in function list()
my_list = [[],[],[],[],[],[]]
my_list[0].append(60)
my_list[1].append(47)
my_list[2].append(0)
my_list[3].append(47)
print my_list # prints [[60],[47],[0],[47],[],[]]

Categories