Create vars based on number of elements on the list

0 votes
I am trying to create a python logic such that it creates variables based on the number of elements in the list. How do I do that?
Jul 23 in Python by Ashish
22 views

1 answer to this question.

0 votes

Hey @Ashish, you can try something like this:

list = [1, 2, 3, 4]
d = {'var%d' % el: None for el in list}
d
{'var4': None, 'var1': None, 'var3': None, 'var2': None}
answered Jul 23 by Haseeb

Related Questions In Python

0 votes
1 answer

Return a list inside a for loop while iterating over the elements of another list

The print() is getting called multiple times ...READ MORE

answered Sep 21, 2018 in Python by SDeb
• 13,210 points
79 views
0 votes
1 answer

How to get the multiplication table of any number using list comprehension?

Hey @Neha, you can use something like ...READ MORE

answered Jul 8 in Python by Sakshi
81 views
+1 vote
3 answers

Write a for loop that prints all elements of a list and their position in the list. a = [4,7,3,2,5,9]

Try using this question by list comprehension: a=[4,7,3,2,5,9] print([x for ...READ MORE

answered 1 day ago in Python by vinaykumar
• 160 points
626 views
+1 vote
4 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
2,078 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 407 views
+4 votes
6 answers
0 votes
1 answer

How to count the number of elements in a list?

To count the number of elements of ...READ MORE

answered May 27 in Python by Nisa
• 1,090 points
33 views
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,720 points
89 views