How to add count for each unique val in list

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












Assume I have a list.



temp = ['A', 'B', 'A', 'B', 'A', 'B']


I am looking for a way to join the count of the string inside.



Intended Output:



['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


I was able to solve it by using a list comprehension but I am looking for a way where I don't have to specify the list [1, 1, 2, 2, 3, 3]. Is it possible?



[j + "_" + str(i) for i, j in zip([1, 1, 2, 2, 3, 3], temp)]









share|improve this question



























    up vote
    7
    down vote

    favorite












    Assume I have a list.



    temp = ['A', 'B', 'A', 'B', 'A', 'B']


    I am looking for a way to join the count of the string inside.



    Intended Output:



    ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


    I was able to solve it by using a list comprehension but I am looking for a way where I don't have to specify the list [1, 1, 2, 2, 3, 3]. Is it possible?



    [j + "_" + str(i) for i, j in zip([1, 1, 2, 2, 3, 3], temp)]









    share|improve this question

























      up vote
      7
      down vote

      favorite









      up vote
      7
      down vote

      favorite











      Assume I have a list.



      temp = ['A', 'B', 'A', 'B', 'A', 'B']


      I am looking for a way to join the count of the string inside.



      Intended Output:



      ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


      I was able to solve it by using a list comprehension but I am looking for a way where I don't have to specify the list [1, 1, 2, 2, 3, 3]. Is it possible?



      [j + "_" + str(i) for i, j in zip([1, 1, 2, 2, 3, 3], temp)]









      share|improve this question















      Assume I have a list.



      temp = ['A', 'B', 'A', 'B', 'A', 'B']


      I am looking for a way to join the count of the string inside.



      Intended Output:



      ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


      I was able to solve it by using a list comprehension but I am looking for a way where I don't have to specify the list [1, 1, 2, 2, 3, 3]. Is it possible?



      [j + "_" + str(i) for i, j in zip([1, 1, 2, 2, 3, 3], temp)]






      python string list counter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      jpp

      77k184491




      77k184491










      asked 2 hours ago









      Rakesh

      764




      764






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          12
          down vote



          accepted










          You can use collections.defaultdict with a for loop:



          from collections import defaultdict

          L = ['A', 'B', 'A', 'B', 'A', 'B']

          dd = defaultdict(int)

          res =
          for item in L:
          dd[item] += 1
          res.append(f'item_dd[item]')

          print(res)

          ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





          share|improve this answer




















          • Thank you :). Is it possible to write this in a list comprehension?
            – Rakesh
            1 hour ago











          • @Rakesh yes, but only using very ugly side effects.
            – timgeb
            1 hour ago






          • 3




            You would have to use boolean expressions inside the comprehension where the evaluation of the truthyness of its parts also calls methods like __setitem__. Trust me, you don't want a listcomp here.
            – timgeb
            1 hour ago







          • 1




            Agree with @timgeb here, a list comprehension would work if you pre-calculated the index values (which would be overly expensive). But not when you need to calculate them based on prior values.
            – jpp
            1 hour ago







          • 2




            @Rakesh Well, since you asked, for science... dd = defaultdict(int); [dd.__setitem__(c, dd[c] + 1) or '_'.format(c, dd[c]) for c in L]. Please use the normal for loop...
            – timgeb
            1 hour ago


















          up vote
          8
          down vote













          You can use a Counter or a defaultdict(int) to keep track of how many times a character has been seen as you encounter them.



          >>> from collections import Counter
          >>>
          >>> temp = ['A', 'B', 'A', 'B', 'A', 'B']
          >>> seen = Counter()
          >>>
          >>> result =
          >>> for c in temp:
          ...: seen.update(c)
          ...: result.append('_'.format(c, seen[c]))
          ...:
          >>> result
          >>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


          Note that seen.update(c) might have unexpected results if you expect strings with more than one character in temp. Demo:



          >>> seen = Counter()
          >>> seen.update('ABC')
          >>> seen
          >>> Counter('A': 1, 'B': 1, 'C': 1)


          Depending on how you want to count and what kind of data you expect, you might want to use the line



          seen[c] += 1


          instead of



          seen.update(c)


          Alternatively, without any imports:



          >>> seen = 
          >>> result =
          >>>
          >>> for c in temp:
          ...: seen[c] = seen.get(c, 0) + 1
          ...: result.append('_'.format(c, seen[c]))
          ...:
          >>> result
          >>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





          share|improve this answer





























            up vote
            4
            down vote













            You can use a dictionary (or better yet, a collections.defaultdict) to maintain the counts for each item:



            from collections import defaultdict

            lst = ['A', 'B', 'A', 'B', 'A', 'B']
            lst2 =
            d = defaultdict(int)

            for item in lst:
            d[item] += 1
            lst2.append('_'.format(item, d[item]))

            print(lst2) # ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


            To use a list comprehension here you'd need some way to update state (i.e. the counters) for each item as you iterate the original list. For that you could use a function with a default argument, e.g.:



            def get_count(item, d=defaultdict(int)):
            d[item] += 1
            return '_'.format(item, d[item])

            lst2 = [get_count(item) for item in lst]
            print(lst2) # ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





            share|improve this answer





























              up vote
              0
              down vote













              This is the fastest way to get the desired output in form of a dict.



              def countElement(a):
              g =
              for i in a:
              if i in g: g[i] +=1
              else: g[i] =1
              return g

              z = [1,1,1,1,2,2,2,2,3,3,4,5,5,234,23,3,12,3,123,12,31,23,13,2,4,23,42,42,34,234,23,42,34,23,423,42,34,23,423,4,234,23,42,34,23,4,23,423,4,23,4]

              print (countElement(z))





              share|improve this answer








              New contributor




              Akash Swain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.

















                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53098757%2fhow-to-add-count-for-each-unique-val-in-list%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                12
                down vote



                accepted










                You can use collections.defaultdict with a for loop:



                from collections import defaultdict

                L = ['A', 'B', 'A', 'B', 'A', 'B']

                dd = defaultdict(int)

                res =
                for item in L:
                dd[item] += 1
                res.append(f'item_dd[item]')

                print(res)

                ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





                share|improve this answer




















                • Thank you :). Is it possible to write this in a list comprehension?
                  – Rakesh
                  1 hour ago











                • @Rakesh yes, but only using very ugly side effects.
                  – timgeb
                  1 hour ago






                • 3




                  You would have to use boolean expressions inside the comprehension where the evaluation of the truthyness of its parts also calls methods like __setitem__. Trust me, you don't want a listcomp here.
                  – timgeb
                  1 hour ago







                • 1




                  Agree with @timgeb here, a list comprehension would work if you pre-calculated the index values (which would be overly expensive). But not when you need to calculate them based on prior values.
                  – jpp
                  1 hour ago







                • 2




                  @Rakesh Well, since you asked, for science... dd = defaultdict(int); [dd.__setitem__(c, dd[c] + 1) or '_'.format(c, dd[c]) for c in L]. Please use the normal for loop...
                  – timgeb
                  1 hour ago















                up vote
                12
                down vote



                accepted










                You can use collections.defaultdict with a for loop:



                from collections import defaultdict

                L = ['A', 'B', 'A', 'B', 'A', 'B']

                dd = defaultdict(int)

                res =
                for item in L:
                dd[item] += 1
                res.append(f'item_dd[item]')

                print(res)

                ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





                share|improve this answer




















                • Thank you :). Is it possible to write this in a list comprehension?
                  – Rakesh
                  1 hour ago











                • @Rakesh yes, but only using very ugly side effects.
                  – timgeb
                  1 hour ago






                • 3




                  You would have to use boolean expressions inside the comprehension where the evaluation of the truthyness of its parts also calls methods like __setitem__. Trust me, you don't want a listcomp here.
                  – timgeb
                  1 hour ago







                • 1




                  Agree with @timgeb here, a list comprehension would work if you pre-calculated the index values (which would be overly expensive). But not when you need to calculate them based on prior values.
                  – jpp
                  1 hour ago







                • 2




                  @Rakesh Well, since you asked, for science... dd = defaultdict(int); [dd.__setitem__(c, dd[c] + 1) or '_'.format(c, dd[c]) for c in L]. Please use the normal for loop...
                  – timgeb
                  1 hour ago













                up vote
                12
                down vote



                accepted







                up vote
                12
                down vote



                accepted






                You can use collections.defaultdict with a for loop:



                from collections import defaultdict

                L = ['A', 'B', 'A', 'B', 'A', 'B']

                dd = defaultdict(int)

                res =
                for item in L:
                dd[item] += 1
                res.append(f'item_dd[item]')

                print(res)

                ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





                share|improve this answer












                You can use collections.defaultdict with a for loop:



                from collections import defaultdict

                L = ['A', 'B', 'A', 'B', 'A', 'B']

                dd = defaultdict(int)

                res =
                for item in L:
                dd[item] += 1
                res.append(f'item_dd[item]')

                print(res)

                ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                jpp

                77k184491




                77k184491











                • Thank you :). Is it possible to write this in a list comprehension?
                  – Rakesh
                  1 hour ago











                • @Rakesh yes, but only using very ugly side effects.
                  – timgeb
                  1 hour ago






                • 3




                  You would have to use boolean expressions inside the comprehension where the evaluation of the truthyness of its parts also calls methods like __setitem__. Trust me, you don't want a listcomp here.
                  – timgeb
                  1 hour ago







                • 1




                  Agree with @timgeb here, a list comprehension would work if you pre-calculated the index values (which would be overly expensive). But not when you need to calculate them based on prior values.
                  – jpp
                  1 hour ago







                • 2




                  @Rakesh Well, since you asked, for science... dd = defaultdict(int); [dd.__setitem__(c, dd[c] + 1) or '_'.format(c, dd[c]) for c in L]. Please use the normal for loop...
                  – timgeb
                  1 hour ago

















                • Thank you :). Is it possible to write this in a list comprehension?
                  – Rakesh
                  1 hour ago











                • @Rakesh yes, but only using very ugly side effects.
                  – timgeb
                  1 hour ago






                • 3




                  You would have to use boolean expressions inside the comprehension where the evaluation of the truthyness of its parts also calls methods like __setitem__. Trust me, you don't want a listcomp here.
                  – timgeb
                  1 hour ago







                • 1




                  Agree with @timgeb here, a list comprehension would work if you pre-calculated the index values (which would be overly expensive). But not when you need to calculate them based on prior values.
                  – jpp
                  1 hour ago







                • 2




                  @Rakesh Well, since you asked, for science... dd = defaultdict(int); [dd.__setitem__(c, dd[c] + 1) or '_'.format(c, dd[c]) for c in L]. Please use the normal for loop...
                  – timgeb
                  1 hour ago
















                Thank you :). Is it possible to write this in a list comprehension?
                – Rakesh
                1 hour ago





                Thank you :). Is it possible to write this in a list comprehension?
                – Rakesh
                1 hour ago













                @Rakesh yes, but only using very ugly side effects.
                – timgeb
                1 hour ago




                @Rakesh yes, but only using very ugly side effects.
                – timgeb
                1 hour ago




                3




                3




                You would have to use boolean expressions inside the comprehension where the evaluation of the truthyness of its parts also calls methods like __setitem__. Trust me, you don't want a listcomp here.
                – timgeb
                1 hour ago





                You would have to use boolean expressions inside the comprehension where the evaluation of the truthyness of its parts also calls methods like __setitem__. Trust me, you don't want a listcomp here.
                – timgeb
                1 hour ago





                1




                1




                Agree with @timgeb here, a list comprehension would work if you pre-calculated the index values (which would be overly expensive). But not when you need to calculate them based on prior values.
                – jpp
                1 hour ago





                Agree with @timgeb here, a list comprehension would work if you pre-calculated the index values (which would be overly expensive). But not when you need to calculate them based on prior values.
                – jpp
                1 hour ago





                2




                2




                @Rakesh Well, since you asked, for science... dd = defaultdict(int); [dd.__setitem__(c, dd[c] + 1) or '_'.format(c, dd[c]) for c in L]. Please use the normal for loop...
                – timgeb
                1 hour ago





                @Rakesh Well, since you asked, for science... dd = defaultdict(int); [dd.__setitem__(c, dd[c] + 1) or '_'.format(c, dd[c]) for c in L]. Please use the normal for loop...
                – timgeb
                1 hour ago













                up vote
                8
                down vote













                You can use a Counter or a defaultdict(int) to keep track of how many times a character has been seen as you encounter them.



                >>> from collections import Counter
                >>>
                >>> temp = ['A', 'B', 'A', 'B', 'A', 'B']
                >>> seen = Counter()
                >>>
                >>> result =
                >>> for c in temp:
                ...: seen.update(c)
                ...: result.append('_'.format(c, seen[c]))
                ...:
                >>> result
                >>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


                Note that seen.update(c) might have unexpected results if you expect strings with more than one character in temp. Demo:



                >>> seen = Counter()
                >>> seen.update('ABC')
                >>> seen
                >>> Counter('A': 1, 'B': 1, 'C': 1)


                Depending on how you want to count and what kind of data you expect, you might want to use the line



                seen[c] += 1


                instead of



                seen.update(c)


                Alternatively, without any imports:



                >>> seen = 
                >>> result =
                >>>
                >>> for c in temp:
                ...: seen[c] = seen.get(c, 0) + 1
                ...: result.append('_'.format(c, seen[c]))
                ...:
                >>> result
                >>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





                share|improve this answer


























                  up vote
                  8
                  down vote













                  You can use a Counter or a defaultdict(int) to keep track of how many times a character has been seen as you encounter them.



                  >>> from collections import Counter
                  >>>
                  >>> temp = ['A', 'B', 'A', 'B', 'A', 'B']
                  >>> seen = Counter()
                  >>>
                  >>> result =
                  >>> for c in temp:
                  ...: seen.update(c)
                  ...: result.append('_'.format(c, seen[c]))
                  ...:
                  >>> result
                  >>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


                  Note that seen.update(c) might have unexpected results if you expect strings with more than one character in temp. Demo:



                  >>> seen = Counter()
                  >>> seen.update('ABC')
                  >>> seen
                  >>> Counter('A': 1, 'B': 1, 'C': 1)


                  Depending on how you want to count and what kind of data you expect, you might want to use the line



                  seen[c] += 1


                  instead of



                  seen.update(c)


                  Alternatively, without any imports:



                  >>> seen = 
                  >>> result =
                  >>>
                  >>> for c in temp:
                  ...: seen[c] = seen.get(c, 0) + 1
                  ...: result.append('_'.format(c, seen[c]))
                  ...:
                  >>> result
                  >>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





                  share|improve this answer
























                    up vote
                    8
                    down vote










                    up vote
                    8
                    down vote









                    You can use a Counter or a defaultdict(int) to keep track of how many times a character has been seen as you encounter them.



                    >>> from collections import Counter
                    >>>
                    >>> temp = ['A', 'B', 'A', 'B', 'A', 'B']
                    >>> seen = Counter()
                    >>>
                    >>> result =
                    >>> for c in temp:
                    ...: seen.update(c)
                    ...: result.append('_'.format(c, seen[c]))
                    ...:
                    >>> result
                    >>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


                    Note that seen.update(c) might have unexpected results if you expect strings with more than one character in temp. Demo:



                    >>> seen = Counter()
                    >>> seen.update('ABC')
                    >>> seen
                    >>> Counter('A': 1, 'B': 1, 'C': 1)


                    Depending on how you want to count and what kind of data you expect, you might want to use the line



                    seen[c] += 1


                    instead of



                    seen.update(c)


                    Alternatively, without any imports:



                    >>> seen = 
                    >>> result =
                    >>>
                    >>> for c in temp:
                    ...: seen[c] = seen.get(c, 0) + 1
                    ...: result.append('_'.format(c, seen[c]))
                    ...:
                    >>> result
                    >>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





                    share|improve this answer














                    You can use a Counter or a defaultdict(int) to keep track of how many times a character has been seen as you encounter them.



                    >>> from collections import Counter
                    >>>
                    >>> temp = ['A', 'B', 'A', 'B', 'A', 'B']
                    >>> seen = Counter()
                    >>>
                    >>> result =
                    >>> for c in temp:
                    ...: seen.update(c)
                    ...: result.append('_'.format(c, seen[c]))
                    ...:
                    >>> result
                    >>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


                    Note that seen.update(c) might have unexpected results if you expect strings with more than one character in temp. Demo:



                    >>> seen = Counter()
                    >>> seen.update('ABC')
                    >>> seen
                    >>> Counter('A': 1, 'B': 1, 'C': 1)


                    Depending on how you want to count and what kind of data you expect, you might want to use the line



                    seen[c] += 1


                    instead of



                    seen.update(c)


                    Alternatively, without any imports:



                    >>> seen = 
                    >>> result =
                    >>>
                    >>> for c in temp:
                    ...: seen[c] = seen.get(c, 0) + 1
                    ...: result.append('_'.format(c, seen[c]))
                    ...:
                    >>> result
                    >>> ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 2 hours ago

























                    answered 2 hours ago









                    timgeb

                    40.9k105580




                    40.9k105580




















                        up vote
                        4
                        down vote













                        You can use a dictionary (or better yet, a collections.defaultdict) to maintain the counts for each item:



                        from collections import defaultdict

                        lst = ['A', 'B', 'A', 'B', 'A', 'B']
                        lst2 =
                        d = defaultdict(int)

                        for item in lst:
                        d[item] += 1
                        lst2.append('_'.format(item, d[item]))

                        print(lst2) # ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


                        To use a list comprehension here you'd need some way to update state (i.e. the counters) for each item as you iterate the original list. For that you could use a function with a default argument, e.g.:



                        def get_count(item, d=defaultdict(int)):
                        d[item] += 1
                        return '_'.format(item, d[item])

                        lst2 = [get_count(item) for item in lst]
                        print(lst2) # ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





                        share|improve this answer


























                          up vote
                          4
                          down vote













                          You can use a dictionary (or better yet, a collections.defaultdict) to maintain the counts for each item:



                          from collections import defaultdict

                          lst = ['A', 'B', 'A', 'B', 'A', 'B']
                          lst2 =
                          d = defaultdict(int)

                          for item in lst:
                          d[item] += 1
                          lst2.append('_'.format(item, d[item]))

                          print(lst2) # ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


                          To use a list comprehension here you'd need some way to update state (i.e. the counters) for each item as you iterate the original list. For that you could use a function with a default argument, e.g.:



                          def get_count(item, d=defaultdict(int)):
                          d[item] += 1
                          return '_'.format(item, d[item])

                          lst2 = [get_count(item) for item in lst]
                          print(lst2) # ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





                          share|improve this answer
























                            up vote
                            4
                            down vote










                            up vote
                            4
                            down vote









                            You can use a dictionary (or better yet, a collections.defaultdict) to maintain the counts for each item:



                            from collections import defaultdict

                            lst = ['A', 'B', 'A', 'B', 'A', 'B']
                            lst2 =
                            d = defaultdict(int)

                            for item in lst:
                            d[item] += 1
                            lst2.append('_'.format(item, d[item]))

                            print(lst2) # ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


                            To use a list comprehension here you'd need some way to update state (i.e. the counters) for each item as you iterate the original list. For that you could use a function with a default argument, e.g.:



                            def get_count(item, d=defaultdict(int)):
                            d[item] += 1
                            return '_'.format(item, d[item])

                            lst2 = [get_count(item) for item in lst]
                            print(lst2) # ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']





                            share|improve this answer














                            You can use a dictionary (or better yet, a collections.defaultdict) to maintain the counts for each item:



                            from collections import defaultdict

                            lst = ['A', 'B', 'A', 'B', 'A', 'B']
                            lst2 =
                            d = defaultdict(int)

                            for item in lst:
                            d[item] += 1
                            lst2.append('_'.format(item, d[item]))

                            print(lst2) # ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']


                            To use a list comprehension here you'd need some way to update state (i.e. the counters) for each item as you iterate the original list. For that you could use a function with a default argument, e.g.:



                            def get_count(item, d=defaultdict(int)):
                            d[item] += 1
                            return '_'.format(item, d[item])

                            lst2 = [get_count(item) for item in lst]
                            print(lst2) # ['A_1', 'B_1', 'A_2', 'B_2', 'A_3', 'B_3']






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 1 hour ago

























                            answered 2 hours ago









                            Eugene Yarmash

                            80.9k21170255




                            80.9k21170255




















                                up vote
                                0
                                down vote













                                This is the fastest way to get the desired output in form of a dict.



                                def countElement(a):
                                g =
                                for i in a:
                                if i in g: g[i] +=1
                                else: g[i] =1
                                return g

                                z = [1,1,1,1,2,2,2,2,3,3,4,5,5,234,23,3,12,3,123,12,31,23,13,2,4,23,42,42,34,234,23,42,34,23,423,42,34,23,423,4,234,23,42,34,23,4,23,423,4,23,4]

                                print (countElement(z))





                                share|improve this answer








                                New contributor




                                Akash Swain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.





















                                  up vote
                                  0
                                  down vote













                                  This is the fastest way to get the desired output in form of a dict.



                                  def countElement(a):
                                  g =
                                  for i in a:
                                  if i in g: g[i] +=1
                                  else: g[i] =1
                                  return g

                                  z = [1,1,1,1,2,2,2,2,3,3,4,5,5,234,23,3,12,3,123,12,31,23,13,2,4,23,42,42,34,234,23,42,34,23,423,42,34,23,423,4,234,23,42,34,23,4,23,423,4,23,4]

                                  print (countElement(z))





                                  share|improve this answer








                                  New contributor




                                  Akash Swain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.



















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    This is the fastest way to get the desired output in form of a dict.



                                    def countElement(a):
                                    g =
                                    for i in a:
                                    if i in g: g[i] +=1
                                    else: g[i] =1
                                    return g

                                    z = [1,1,1,1,2,2,2,2,3,3,4,5,5,234,23,3,12,3,123,12,31,23,13,2,4,23,42,42,34,234,23,42,34,23,423,42,34,23,423,4,234,23,42,34,23,4,23,423,4,23,4]

                                    print (countElement(z))





                                    share|improve this answer








                                    New contributor




                                    Akash Swain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    This is the fastest way to get the desired output in form of a dict.



                                    def countElement(a):
                                    g =
                                    for i in a:
                                    if i in g: g[i] +=1
                                    else: g[i] =1
                                    return g

                                    z = [1,1,1,1,2,2,2,2,3,3,4,5,5,234,23,3,12,3,123,12,31,23,13,2,4,23,42,42,34,234,23,42,34,23,423,42,34,23,423,4,234,23,42,34,23,4,23,423,4,23,4]

                                    print (countElement(z))






                                    share|improve this answer








                                    New contributor




                                    Akash Swain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    share|improve this answer



                                    share|improve this answer






                                    New contributor




                                    Akash Swain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    answered 32 mins ago









                                    Akash Swain

                                    1




                                    1




                                    New contributor




                                    Akash Swain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.





                                    New contributor





                                    Akash Swain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






                                    Akash Swain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53098757%2fhow-to-add-count-for-each-unique-val-in-list%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        What does second last employer means? [closed]

                                        List of Gilmore Girls characters

                                        Confectionery